1

I have created a functionality where I get the geometry data from the shapefile and display it on the map and table structure.

Here there is no restriction like that only some specific files can upload, Here we upload any .shp file with any projection system.

I used code like this:

FileDataStore myData = FileDataStoreFinder.getDataStore(new File(shapeFileName)); //shapeFileName is file name
SimpleFeatureSource source = myData.getFeatureSource();

Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem sourceCRS = (source.getSchema().getCoordinateReferenceSystem() != null ? source.getSchema().getCoordinateReferenceSystem() : factory.createCoordinateReferenceSystem("4326"));
MathTransform transform = CRS.findMathTransform(sourceCRS, factory.createCoordinateReferenceSystem("3857"), true);

FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
try (FeatureIterator<SimpleFeature> features = collection.features()) {
  while (features.hasNext()) {
    SimpleFeature feature = features.next();
        // Transform the geometry which is default for my system
        feature.setDefaultGeometry(JTS.transform((Geometry) feature.getDefaultGeometry(), transform));
        simpleFeatures.add(feature);
    }
}

Here I am going to fetch all the geometry data in a list after the transform. Most of the data are working correctly but there is an issue with one projection system.

PROJCS["British_National_Grid",
  GEOGCS["GCS_OSGB_1936",
    DATUM["D_OSGB_1936",
      SPHEROID["Airy_1830", 6377563.396, 299.3249646]],
    PRIMEM["Greenwich", 0.0],
    UNIT["degree", 0.017453292519943295],
    AXIS["Longitude", EAST],
    AXIS["Latitude", NORTH]],
  PROJECTION["Transverse_Mercator"],
  PARAMETER["central_meridian", -2.0],
  PARAMETER["latitude_of_origin", 49.0],
  PARAMETER["scale_factor", 0.9996012717],
  PARAMETER["false_easting", 400000.0],
  PARAMETER["false_northing", -100000.0],
  UNIT["m", 1.0],
  AXIS["x", EAST],
  AXIS["y", NORTH]]

Whenever I try to convert the projection into EPSG:3857 or EPSG:4326 it is not getting accurate. The data is the slightly right position from the accurate position.

Please check the screenshot. RED geometry is shown after the transform but the accurate geometry I have marked with the LIGHT BLUE line.enter image description here

1 Answer 1

1

You don't say how you acquired your "accurate" polygon in the question but my suspicion is that you transformed it using a NTv2 grid while your GeoTools code is not.

See this answer for example code on how to check which transform is being used.

You might also want to look at using a ReprojectingFeatureCollection to handle the reprojection for you.

2
  • Hi lan, Thanks for the speedy reply. I have already tried with the ReprojectingFeatureCollection and it gives the same problem. I have opened the .shp files in QGIS based on that I have drawn the accurate polygon. In the post, I have added the content of the .prj file of the .shp file. Here I am not going to upload only this file I have multiple files with different projection systems (e.g. EPSG:4326, EPSG:3857). So is it work with the NTv2 grid file?
    – Nirav
    Commented Dec 1, 2021 at 5:58
  • I wasn't suggesting that the ReprojectingFeatureCollection would give a different answer just that it is easier to use than your own interator, So you have no "proof" that the "accurate" polygon is correct. Try adding the grid file and see - I think QGIS comes with that by default now. Otherwise I would avoid moving from 27700 as there will always be some loss of accuracy.
    – Ian Turton
    Commented Dec 1, 2021 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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