使用功能将jts转换为GeoJSON时发生类型错误

问题描述 投票:0回答:1

我有一个带有GIS Geometry的Java POJO“任务”,我正尝试将其转换为具有功能的GeoJSON。

我正在用来进行转换的库是jts2geojson

我的测试用例代码是这个,当我尝试将其传递给阅读器时,错误在String geoJson上。

    GeometryFactory gf = new GeometryFactory();
    Double y = -36.829;
    Double x = 174.896;

    Task testTask = new Task();
    testTask.setId(new Long(01));
    testTask.setTitle("Test Task");
    Point p = gf.createPoint(new Coordinate(x, y));
    p.setSRID(4326);
    testTask.setLocation(p);
    taskRepository.save(testTask);

    ObjectMapper objectMapper = new ObjectMapper();
    String geoJson = objectMapper.writeValueAsString(testTask); // geojson is a String

    GeoJSONReader reader = new GeoJSONReader();
    org.wololo.geojson.Geometry geometry = reader.read(geoJson); // type error

    // create and serialize a FeatureCollection
    List<Feature> features = new ArrayList<Feature>();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("Size", 5);
    properties.put("Area", "Auckland");
    features.add(new Feature(geoJson, properties));
    GeoJSONWriter writer = new GeoJSONWriter();
    GeoJson json = writer.write(features);

[问题是在org.wololo.geojson.Geometry geometry = reader.read(geoJson);行上的类型错误。

enter image description here

即使reader.read方法采用GeoJSON字符串,它也不接受字符串。

我也尝试过传递JTS点p:

        GeoJSONReader reader = new GeoJSONReader();
    org.wololo.geojson.Geometry geometry = reader.read(p);

但是这也会在IDE中导致错误:

enter image description here

java gis geojson
1个回答
0
投票

我有错误的读者和作家方法。作者从jts.geometry转到wololo.geometry。

    GeometryFactory gf = new GeometryFactory();
    Double y = -36.829;
    Double x = 174.896;

    Task testTask = new Task();
    testTask.setId(new Long(01));
    testTask.setTitle("Test Task");
    Point p = gf.createPoint(new Coordinate(x, y));
    p.setSRID(4326);
    testTask.setLocation(p);
    taskRepository.save(testTask);

    ObjectMapper objectMapper = new ObjectMapper();
    String geoJson = objectMapper.writeValueAsString(testTask);

    System.out.println(geoJson);

    GeoJSONWriter writer = new GeoJSONWriter();
    org.wololo.geojson.Geometry geom = writer.write(p);

    // create and serialize a FeatureCollection
    List<Feature> features = new ArrayList<Feature>();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("Size", 5);
    properties.put("Area", "Auckland");
    features.add(new Feature(geom, properties));
    GeoJSONWriter writer1 = new GeoJSONWriter();
    FeatureCollection json = writer1.write(features);

writer方法将我的点p转换为org.wololo.geojson.geometry对象,然后可以将其添加到Feature或FeatureCollection。

© www.soinside.com 2019 - 2024. All rights reserved.