我需要使用上述坐标系将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();
    }


以下代码更相关:如何检查点是否在形状内

    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多边形进行比较?

最佳答案

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


(我尚未实际编译此代码,但它应该可以工作-有关更多详细信息,请参见tutorial)。

关于java - 将ED_1950_UTM_Zone_32N(23032)转换为WGS84,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25040949/

10-13 00:25