2

我需要将使用上述坐标系的 Shapefile 导入并处理到使用 WGS84 坐标系的 Java 应用程序中。

更具体地说,我读取 shapefile 并提取多边形,并将每个多边形存储到我的数据库中的一个对象中。稍后,我将不得不将空间点与这些多边形进行比较(检查点是否在其多边形之内或之外)。点和多边形位于不同的 CRS 中的事实似乎是我达到 0 次命中的原因。

虽然我认为这无关紧要,但这是我使用 GeoTools 读取 shapefile 的方式:

        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();
    }

下面的代码更相关: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));

如何将多边形从 ED_1950... 转换为 WGS84?或者,如何将 WGS84 点与 ED_1950 多边形进行比较?

4

1 回答 1

0

首先,您需要从丹麦转换为 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);

然后对于您添加到数据库中的每个几何图形,您需要对其进行转换:

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

(我实际上并没有编译此代码,但它应该可以工作 - 有关更多详细信息,请参阅本教程)。

于 2014-07-31T09:53:42.840 回答