2

I need to import and treat a Shapefile using the above mentioned coordinate system into a Java application that uses WGS84 coordinate system.

More specifically, I read the shapefile and extract polygons, and store each polygon to an object in my database. Later, I'll have to compare spatial points with those polygons (checking if point is in or out its polygon). The fact that the points and the polygons are in different CRSes seems to be the reason why I reach 0 hits.

While I think it is irrelevant, here is how I read the shapefile with GeoTools:

        InputStream stream = new FileInputStream("path/to/file");

        File tempDir = File.createTempFile("shapefile_temp", "");

        if (tempDir.exists())
            tempDir.delete();
        tempDir.mkdir();

        URL shpName = null;

        Files.unzip(stream, tempDir);

        for (File file : tempDir.listFiles())
            if (file.getName()
                    .endsWith(".shp"))
            {
                shpName = file.toURI()
                              .toURL();
                break;
            }

        if (shpName == null)
            throw new RuntimeException("No SHP found");

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("url", shpName);
        DataStore dataStore = DataStoreFinder.getDataStore(map);

        try
        {
            String typeName = dataStore.getTypeNames()[0];

            FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);

            FeatureIterator<SimpleFeature> iterator = source.getFeatures()
                                                            .features();

            while (iterator.hasNext())
            {

                Feature feature = iterator.next();

                System.out.println();
                System.out.println((int) feature.getProperty("COD_PRO")
                                                .getValue() + "\t" + feature.getProperty("NOME_PRO")
                                                                            .getValue());
                System.out.println(feature.getDefaultGeometryProperty()
                                          .getValue()); //This thing will be stored in a CQEngine and compared against a given point
            }

        }
        finally
        {
            tempDir.delete();
        }

    }
    finally
    {
        System.gc();
    }

The following code is more relevant: how to check if a point is inside the shape

    final GeometryFactory geoFactory = new GeometryFactory();
    com.vividsolutions.jts.geom.Point point = geoFactory.createPoint(new Coordinate(loc.getLongitude().doubleValue(), loc.getLatitude().doubleValue()));

                            loc.setContained(shape.getGeometry().covers(pointBing) || shape.getGeometry().contains(point));

How to convert polygons from ED_1950... to WGS84? Or, how to compare a WGS84 point with a ED_1950 polygon?

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305

1 Answers1

0

First you need a transform from Denmark to WGS84:

CoordinateReferenceSystem worldCRS = CRS.decode("EPSG:4326");
CoordinateReferenceSystem ed_1950 = CRS.decode("EPSG:23032");
boolean lenient = true; // allow for some error due to different datums
MathTransform transform = CRS.findMathTransform(ed_1950, worldCRS, lenient);

Then for each Geometry that you add to your database you need to transform it:

Geometry geometry = (Geometry) feature.getDefaultGeometry();
Geometry geometry2 = JTS.transform(geometry, transform);

(I haven't actually compiled this code but it should work - see this tutorial for more details).

Ian Turton
  • 10,018
  • 1
  • 28
  • 47