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.