Hi Jan,
thanks for your reply,
I have supposed that what i want to do is not really simple and for that i
found the code below:
I get an error on line 82
82: *List<Name> names = data.getNames();*
*Exception in thread "main" java.lang.NullPointerException
at SnapToLine.main(SnapToLine.java:82)
data.getNames(); return null
cause I think the DataStore data = DataStoreFinder.getDataStore(params);
can't parse the shapefile that i get from openstreetmap*
I try to undestand what kind of file I need for the code in example,
http://svn.geotools.org/trunk/demo/example/src/main/java/org/geotools/demo/jts/SnapToLine.java
http://svn.geotools.org/trunk/demo/example/src/main/java/org/geotools/demo/jts/SnapToLine.java
I was looking also some script or apps to convert shp in csv or xml without
success.
Probably my approach to solve the problem is completely wrong but the
description of the snaptoline.java convinced me to get this direction.
Thank you very much for your help
Vittorio
/public class SnapToLine {
public static void main(String[] args) throws Exception {
File file = new File("c:\\marche\\marche.shp");
System.out.println("Snapping against:"+file);
Map<String,Serializable> params = new
HashMap<String,Serializable>();
if( file.getName().endsWith(".properties")){
Properties properties = new Properties();
FileInputStream inStream = new FileInputStream(file);
try {
properties.load( inStream);
}
finally {
inStream.close();
}
for( Map.Entry<Object,Object> property : properties.entrySet()
){
params.put( (String) property.getKey(), (String)
property.getValue() );
}
}
else {
params.put("url", file.toURI().toURL() );
}
System.out.println(params.size());
* DataStore data = DataStoreFinder.getDataStore(params); *
*82 List<Name> names = data.getNames();*
SimpleFeatureSource source = data.getFeatureSource( names.get(0));
final SpatialIndex index = new STRtree();
SimpleFeatureCollection features = source.getFeatures();
System.out.println("Slurping in features ...");
features.accepts( new FeatureVisitor(){
public void visit(Feature feature) {
SimpleFeature simpleFeature = (SimpleFeature) feature;
Geometry geom = (MultiLineString)
simpleFeature.getDefaultGeometry();
Envelope bounds = geom.getEnvelopeInternal();
if( bounds.isNull() ) return; // must be empty geometry?
index.insert( bounds, new LocationIndexedLine( geom ));
}
}, new NullProgressListener() );
final int DURATION = 6000;
System.out.println("we now have our spatial index and are going to
snap for "+DURATION);
ReferencedEnvelope limit = features.getBounds();
Coordinate[] points = new Coordinate[10000];
Random rand = new Random(file.hashCode());
for( int i=0; i<10000;i++){
points[i] = new Coordinate(
limit.getMinX()+rand.nextDouble()*limit.getWidth(),
limit.getMinY()+rand.nextDouble()*limit.getHeight()
);
}
double distance = limit.getSpan(0) / 100.0;
long now = System.currentTimeMillis();
long then = now+DURATION;
int count = 0;
System.out.println("we now have our spatial index and are going to
snap for "+DURATION);
while( System.currentTimeMillis()<then){
Coordinate pt = points[rand.nextInt(10000)];
Envelope search = new Envelope(pt);
search.expandBy(distance);
List<LocationIndexedLine> hits = index.query( search );
double d = Double.MAX_VALUE;
Coordinate best = null;
for( LocationIndexedLine line : hits ){
LinearLocation here = line.project( pt );
Coordinate point = line.extractPoint( here );
double currentD = point.distance( pt );
if( currentD < d ){
best = point;
}
}
if( best == null ){
// we did not manage to snap to a line? with real data sets
this happens all the time...
System.out.println( pt + "-X");
}
else {
System.out.println( pt + "->" + best );
}
count++;
}
System.out.println("snapped "+count+" times - and now I am tired");
System.out.println("snapped "+count/DURATION+" per milli?");
}
} /
--
View this message in context:
http://osgeo-org.1803224.n2.nabble.com/newbie-Snap-a-Point-to-a-Line-problem-with-openstreetmap-shape-file-tp6951329p6953378.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.
------------------------------------------------------------------------------
RSA® Conference 2012
Save $700 by Nov 18
Register now!
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users