Changes between Version 4 and Version 5 of TutorialKMlCoordinates
- Timestamp:
- 07/20/09 14:19:02 (4 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TutorialKMlCoordinates
v4 v5 2 2 3 3 I am going to explain (a) how to retrieve (and print) coordinates of all spatial features in a feature collection, and (b) how to find 4 the nearest point compared to a given point. This tutorial presumes understanding of the Tutorial [wiki:TutorialKMlStreams Parsing KML into the Feature Model]. It also helps to have some basic knowledge about the [http://www.vividsolutions.com/jts/jtshome.htm Java Topology Suite (JTS)] and the concept of [http://en.wikipedia.org/wiki/Great-circle_distance Geodetic (Great Circle) Distances].4 the nearest point out of a list of features compared to a given point. This tutorial presumes understanding of the Tutorial [wiki:TutorialKMlStreams Parsing KML into the Feature Model]. It also helps to have some basic knowledge about the [http://www.vividsolutions.com/jts/jtshome.htm Java Topology Suite (JTS)] and the concept of [http://en.wikipedia.org/wiki/Great-circle_distance Geodetic (Great Circle) Distances]. 5 5 6 6 === Getting the coordinates === … … 24 24 {{{ 25 25 GeometryFactory geomFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326); 26 27 //com.vividsolutions.jts.geom.Point givenPoint = geomFactory.createPoint(...) 28 26 29 double shortestDistance = Double.MAX_VALUE; 30 Point nearestPoint = null; 31 Point actualPoint = null; 32 27 33 for(Feature f : features.listFeaturesRecursively()){ 28 34 Geometry geom = f.getGeometry(); 29 Point givenPoint = ...; // comes from somewhere else30 31 35 Coordinate[] coordinates = geom.getCoordinates(); 32 36 for (int i = 0; i < coordinates.length; i++) { 33 System.out.println(coordinates[i].x+" "+coordinates[i].y); 34 Coordinate[] current = {givenCoordinate, coordinates[i]}; // where does givenCoordinate come from 35 if (new GeodeticDistance().length(geomFactory.createGeometry(current)) < shortestDistance) { 36 // changed to createGeometry, since we don't know what type we have here 37 // it should not be length, but a distance between two geometries 38 shortestDistance = new GeodeticDistance().length(geomFactory.createLineString(actualLine)); // again, should be createGeometry 39 nearestPoint = new Point(coordinates[i].x, coordinates[i].y); 37 actualPoint = geomFactory.createPoint(coordinates[i]); 38 if (new GeodeticDistance().betweenGeometries(givenPoint, actualPoint)<shortestDistance) { 39 shortestDistance = new GeodeticDistance().betweenGeometries(givenPoint, actualPoint); 40 nearestPoint = actualPoint; 40 41 } 41 42 }