Hi Michael,
after many many tests i solve the problem... I Hope :-)
I get the solution using the latest geotools 8.0 with some fix
first of all I used
org.geotools.data.FileDataStore;
to load the shapefile like the example on Quickstart
second i push inside the library the follow api:
gt-main, gt-api, gt-data, gt-opengis, off course
gt-shapefile and jts-1.8
instead to SimpleFeatureSource I used FeatureSource
Now I have to collect data and check if this function permit to fix the
accuracy of gps raw data and snap the postition to the correct road.
Let me know what do you think about
Thanks a lot
Vittorio
import java.io.File;
import java.util.List;
import java.util.Random;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.util.NullProgressListener;
import org.opengis.feature.Feature;
import org.opengis.feature.FeatureVisitor;
import org.opengis.feature.simple.SimpleFeature;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.index.SpatialIndex;
import com.vividsolutions.jts.index.strtree.STRtree;
import com.vividsolutions.jts.linearref.LinearLocation;
import com.vividsolutions.jts.linearref.LocationIndexedLine;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.feature.FeatureCollection;
public class SnapToLine {
public static void main(String[] args) throws Exception {
*
File file = new File("marche_highway.shp");
FileDataStore store = FileDataStoreFinder.getDataStore(file);*
System.out.println("Snapping against:"+file);
FeatureSource source = store.getFeatureSource();
final SpatialIndex index = new STRtree();
FeatureCollection 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-tp6951329p6954870.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