1

I'm attempting to create a Feature based on a Polygon defined by a ReferencedEnvelope of the provided Feature (in this case a country).

However I am seeing a CRS error NoSuchAuthorityCodeException: No code "AUTO:42004,0.0,2351500.0" from authority "AUTO" found for object of type "ProjectedCRS" Why isn't AUTO finding a suitable CRS, and is this is the easiest method of creating the Feature?

public static SimpleFeatureSource boundaryAsFeature(SimpleFeatureSource country) throws TransformException, FactoryException, SchemaException, IOException {
        ReferencedEnvelope referencedEnvelope = Envelopes.expandToInclude(country.getBounds(), 1000.0);

        double x = referencedEnvelope.getMedian(0);
        double y = referencedEnvelope.getMedian(1);
        // reproject to local crs, respecting axis order
        double lon;
        double lat;
        if (CRS.getAxisOrder(referencedEnvelope.getCoordinateReferenceSystem()) == CRS.AxisOrder.EAST_NORTH) {
          lat = x;
          lon = y;
        } else {
          lat = y;
          lon = x;
        }
        String code = "AUTO:42004," + lat + "," + lon;

        CoordinateReferenceSystem auto = CRS.decode(code);
        ReferencedEnvelope bounds = referencedEnvelope.transform(auto, true);

        Integer SRID = CRS.lookupEpsgCode(auto, false);

        // define feature type & collection to hold the envelope shape
        SimpleFeatureType TYPE = DataUtilities.createType("bounding box","geom:Polygon:srid="+SRID);
        DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal",TYPE);

        SimpleFeatureTypeBuilder ftBuilder = new SimpleFeatureTypeBuilder();
        ftBuilder.setCRS(auto);

        Geometry geometry = JTS.toGeometry( bounds );
        featureCollection.add( SimpleFeatureBuilder.build( TYPE, new Object[]{ geometry }, null) );

        return DataUtilities.source( featureCollection );
    }

Edit

I've removed the local projection in the method to simplify things. My code is now like so....

    public static SimpleFeatureSource boundaryAsFeature(SimpleFeatureSource country) throws TransformException, FactoryException, SchemaException, IOException {
        ReferencedEnvelope referencedEnvelope = Envelopes.expandToInclude(country.getBounds(), 1.0);

        // extract the original CRS
        CoordinateReferenceSystem origCRS = country.getFeatures().getSchema().getCoordinateReferenceSystem();

        Integer SRID = CRS.lookupEpsgCode(origCRS, true);

        // define feature type & collection to hold the envelope shape
        SimpleFeatureType TYPE = DataUtilities.createType("bounding box","geom:Polygon:srid="+SRID);
        DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal",TYPE);

        SimpleFeatureTypeBuilder ftBuilder = new SimpleFeatureTypeBuilder();
        ftBuilder.setCRS(origCRS);

        Geometry geometry = JTS.toGeometry( referencedEnvelope );
        featureCollection.add( SimpleFeatureBuilder.build( TYPE, new Object[]{ geometry }, null) );

        return DataUtilities.source( featureCollection );
    }

My input to the method was a country (India) in WGS84...

GEOGCS["GCS_WGS_1984", 
  DATUM["D_WGS_1984", 
    SPHEROID["WGS_1984", 6378137.0, 298.257223563]], 
  PRIMEM["Greenwich", 0.0], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Longitude", EAST], 
  AXIS["Latitude", NORTH]]

With a WorldMap also in WGS84

GEOGCS["WGS84(DD)", 
  DATUM["WGS84", 
    SPHEROID["WGS84", 6378137.0, 298.257223563]], 
  PRIMEM["Greenwich", 0.0], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Geodetic longitude", EAST], 
  AXIS["Geodetic latitude", NORTH]]

However that resulted in bounding box not surrounding India, so I thought the coordinates must have been reversed

Q1) Why is the resulting bounding box not in the right location, although both the Country (India) and world map are in WGS84?

enter image description here

I then tried passing to the method India locally projected...

PROJCS["WGS 84 / Auto Equirectangular", 
  GEOGCS["WGS84(DD)", 
    DATUM["WGS84", 
      SPHEROID["WGS84", 6378137.0, 298.257223563]], 
    PRIMEM["Greenwich", 0.0], 
    UNIT["degree", 0.017453292519943295], 
    AXIS["Geodetic longitude", EAST], 
    AXIS["Geodetic latitude", NORTH]], 
  PROJECTION["Equidistant_Cylindrical"], 
  PARAMETER["central_meridian", 82.75429687499997], 
  PARAMETER["latitude_of_origin", 0.0], 
  PARAMETER["standard_parallel_1", 21.122290039062516], 
  PARAMETER["false_easting", 0.0], 
  PARAMETER["false_northing", 0.0], 
  UNIT["m", 1.0], 
  AXIS["Easting", EAST], 
  AXIS["Northing", NORTH]]

However that results in the CRS lookup (see line below) failing and setting the SRID to null

Integer SRID = CRS.lookupEpsgCode(origCRS, true);

Q2) It looks like the local projection has a WGS84 datum with a projection, so why is the Epsg lookup is failing?

  • 1
    your bounds are in the wrong place because you have the axis order backwards, Looking up auto codes will probably always fail, don't use DataUtilities.createType is not designed to be used in production code only tests. – Ian Turton Sep 27 '18 at 9:23
  • Excellent your code for building the schema has resolved all the issues. Only small issue I'm seeing now is the following warning if a pass in a country already locally projected. 2018-09-27T11:02:19.690+0100 WARNING Possible use of "Equidistant_Cylindrical" projection outside its valid area. Longitude 180°27.0'W is out of range (±180°). Latitude 118°51.0'S is out of range (±90°). – user3188040 Sep 27 '18 at 10:04
  • 1
    somewhere still thinks you are using 4326 when it is not right – Ian Turton Sep 27 '18 at 10:08
  • Found it. I was using JMapFrame.showMap(map); but the bounding box feature was still in a Local projection while the world map & india were in wgs84. I reprojected the bounding box feature to wgs84 and the warning disappeared. Thank you for your amazing help :-) – user3188040 Sep 27 '18 at 10:29
1

You are asking for AUTO:42004,0.0,2351500.0 but the AUTO projections expect a latitude and longitude to follow the AUTO code. 2351500 is not a longitude which would be expected to fall within the range -180 - 180 degrees, hence no projection can be found.

So you need to reproject your feature (or bounds) into WGS84 (EPSG:4326) before taking the centre point. However if your features are already in a suitable planar projection you probably don't need to reproject anything.

EDIT

To build a schema use code like:

SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setName("grid");
tb.add(GridFeatureBuilder.DEFAULT_GEOMETRY_ATTRIBUTE_NAME, Polygon.class, crs);
tb.add("id", Integer.class);
SimpleFeatureType TYPE = tb.buildFeatureType();

This way you don't need to do a lookup on the projection to get the srid code as it just uses the crs object directly.

| improve this answer | |
  • Thank you. I have updated the original question with more information – user3188040 Sep 27 '18 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.