You use the same approach as I had in mind; not all coordinate reference
systems have a good domain of validity. You may want to check for more than one
geographic element (as shown below):
1) create coordinate reference system from wkt
2) look up a matching coordinate reference system from the epsg definitions
(using the CRS utility class)
String code = CRS.lookupIdentifier( example, true ); CoordinateReferenceSystem
crs = CRS.decode( code );
3) ask the CRS.getEnvelope or getGepgraphicBoundingBox as required
Note that CRS.getEnvelope is just a utility method that works best when simple
coordinate reference systems; you may want to sift though *all* of the
"geographic elements" by hand instead of just one?
Here is how CRS.getEnvelope handles things internally...
final Extent domainOfValidity = crs.getDomainOfValidity();
if (domainOfValidity != null) {
for (final GeographicExtent extent : domainOfValidity.getGeographicElements()) {
if (Boolean.FALSE.equals(extent.getInclusion())) {
continue;
}
if (extent instanceof BoundingPolygon) {
for (final Geometry geometry : ((BoundingPolygon) extent).getPolygons()) {
final Envelope candidate = geometry.getEnvelope();
if (candidate != null) {
final CoordinateReferenceSystem sourceCRS =
candidate.getCoordinateReferenceSystem();
if (sourceCRS == null || equalsIgnoreMetadata(sourceCRS, crs)) {
....
}
}
}
}
}
--
Jody Garnett
On Friday, 27 May 2011 at 5:17 PM, Glenn Prigent wrote:
> Hello,
>
> i need to test if a point is in the domain of validity of a coordinate
> system.
>
> The coordinate system is created by parsing a wkt string.
>
> Is there a simple way to test if a point is in this system ?
>
> I try the code below, it works with certain wkt string by not with all.
>
> import java.io.IOException;
>
> import org.geotools.geometry.jts.JTSFactoryFinder;
> import org.geotools.referencing.CRS;
> import org.opengis.metadata.extent.Extent;
> import org.opengis.metadata.extent.GeographicBoundingBox;
> import org.opengis.referencing.crs.CoordinateReferenceSystem;
>
> import com.vividsolutions.jts.geom.Coordinate;
> import com.vividsolutions.jts.geom.GeometryFactory;
> import com.vividsolutions.jts.geom.LinearRing;
> import com.vividsolutions.jts.geom.Point;
> import com.vividsolutions.jts.geom.Polygon;
>
> public class Test {
>
> public static void main(String [] args) throws Exception {
>
> String wktTest = "GEOGCS[" + "\"WGS 84\"," + " DATUM[" + "
> \"WGS_1984\","
> + " SPHEROID[\"WGS
> 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," + "
> TOWGS84[0,0,0,0,0,0,0],"
> + " AUTHORITY[\"EPSG\",\"6326\"]]," + "
> PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
> + "
> UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]]," + "
> AXIS[\"Lat\",NORTH]," + " AXIS[\"Long\",EAST],"
> + " AUTHORITY[\"EPSG\",\"4326\"]]";
>
> String wktTest1 = "PROJCS[\"Belge 1972 / Belge Lambert
> 72\",GEOGCS[\"Belge
> 1972\",DATUM[\"Reseau_National_Belge_1972\",SPHEROID[\"International
> 1924\",6378388.0,297.0,AUTHORITY[\"EPSG\",\"7022\"]],TOWGS84[-99.1,53.3,-112.5,0.419,-0.83,1.885,-1.0],AUTHORITY[\"EPSG\",\"6313\"]],PRIMEM[\"Greenwich\",0.0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943295],AXIS[\"Lon\",EAST],AXIS[\"Lat\",NORTH],AUTHORITY[\"EPSG\",\"4313\"]],PROJECTION[\"Lambert_Conformal_Conic_2SP_Belgium\"],PARAMETER[\"central_meridian\",4.356939722222222],PARAMETER[\"latitude_of_origin\",90.0],PARAMETER[\"standard_parallel_1\",49.833333333333336],PARAMETER[\"false_easting\",150000.01256],PARAMETER[\"false_northing\",5400088.4378],PARAMETER[\"standard_parallel_2\",51.166666666666664],UNIT[\"m\",1.0],AXIS[\"x\",EAST],AXIS[\"y\",NORTH],AUTHORITY[\"EPSG\",\"31300\"]]";
>
> String wktTest2 = "PROJCS[\"ED50 / France
> EuroLambert\",GEOGCS[\"ED50\",DATUM[\"European_Datum_1950\",SPHEROID[\"International
> 1924\",6378388,297,AUTHORITY[\"EPSG\",\"7022\"]],AUTHORITY[\"EPSG\",\"6230\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4230\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Lambert_Conformal_Conic_1SP\"],PARAMETER[\"latitude_of_origin\",46.8],PARAMETER[\"central_meridian\",2.337229166666667],PARAMETER[\"scale_factor\",0.99987742],PARAMETER[\"false_easting\",600000],PARAMETER[\"false_northing\",2200000],AUTHORITY[\"EPSG\",\"2192\"],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]";
>
> String wktTest3 = "PROJCS[\"NTF (Paris) / France II
> (deprecated)\",GEOGCS[\"NTF
> (Paris)\",DATUM[\"Nouvelle_Triangulation_Francaise_Paris\",SPHEROID[\"Clarke
> 1880
> (IGN)\",6378249.2,293.4660212936269,AUTHORITY[\"EPSG\",\"7011\"]],TOWGS84[-168,-60,320,0,0,0,0],AUTHORITY[\"EPSG\",\"6807\"]],PRIMEM[\"Paris\",2.33722917,AUTHORITY[\"EPSG\",\"8903\"]],UNIT[\"grad\",0.01570796326794897,AUTHORITY[\"EPSG\",\"9105\"]],AUTHORITY[\"EPSG\",\"4807\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Lambert_Conformal_Conic_1SP\"],PARAMETER[\"latitude_of_origin\",52],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",0.99987742],PARAMETER[\"false_easting\",600000],PARAMETER[\"false_northing\",2200000],AUTHORITY[\"EPSG\",\"27582\"],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]";
>
> testGeotools(wktTest); // works
> testGeotools(wktTest1); // works
> testGeotools(wktTest2); // exception
> testGeotools(wktTest3); // exception
>
> }
>
> public static void testGeotools(String wkt) throws IOException {
> try {
> // Create a point
> GeometryFactory geometryFactory =
> JTSFactoryFinder.getGeometryFactory(null);
> Point point = geometryFactory.createPoint(new Coordinate(2, 1));
>
> // Parse the wkt string
> CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
> // Try to find the EPSG code from the wkt string
> String code = CRS.lookupIdentifier(crs, true);
> // Get the system from the epsg code
> CoordinateReferenceSystem crs2 = CRS.decode(code);
> // Get the Domain of Validity
> Extent extent = crs2.getDomainOfValidity();
> // Get the bounding box of the extent
> GeographicBoundingBox test = (GeographicBoundingBox)
> extent.getGeographicElements().toArray()[0];
> Coordinate [] coords = new Coordinate [] {new
> Coordinate(test.getWestBoundLongitude(), test.getSouthBoundLatitude()),
> new Coordinate(test.getWestBoundLongitude(),
> test.getNorthBoundLatitude()),
> new Coordinate(test.getEastBoundLongitude(),
> test.getNorthBoundLatitude()),
> new Coordinate(test.getEastBoundLongitude(),
> test.getSouthBoundLatitude()),
> new Coordinate(test.getWestBoundLongitude(),
> test.getSouthBoundLatitude())};
> LinearRing ring = geometryFactory.createLinearRing(coords);
> LinearRing holes[] = null;
> // Create the polygon of the bounding box
> Polygon polygon = geometryFactory.createPolygon(ring, holes);
>
> // Test if the point is in the polygon
> boolean bool = polygon.intersects(point);
>
> System.out.println(bool);
> } catch (Exception e) {
> /* handle some possible errors of of the IOoperations */
> e.printStackTrace();
> }
> }
> }
>
> --
> View this message in context:
> http://osgeo-org.1803224.n2.nabble.com/Point-in-a-domain-of-validity-tp6409999p6409999.html
> Sent from the geotools-gt2-users mailing list archive at Nabble.com
> (http://Nabble.com).
>
> ------------------------------------------------------------------------------
> vRanger cuts backup time in half-while increasing security.
> With the market-leading solution for virtual backup and recovery,
> you get blazing-fast, flexible, and affordable data protection.
> Download your free trial now.
> http://p.sf.net/sfu/quest-d2dcopy1
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> (mailto:[email protected])
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery,
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now.
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users