Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShapesAsWKTModule does not deserialize WKT, returns null #175

Closed
electricsam opened this issue Jun 28, 2019 · 4 comments
Closed

ShapesAsWKTModule does not deserialize WKT, returns null #175

electricsam opened this issue Jun 28, 2019 · 4 comments

Comments

@electricsam
Copy link

The ShapesAsWKTModule serializes Geometry objects just fine, but it cannot deserialize them. The object returned is null.

Here is a test to demonstrate the issue:


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.Test;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.spatial4j.io.jackson.ShapesAsWKTModule;

public class ObjectMapperConfigurationTest {

  public static class GeomWrapper {

    private Geometry geometry;

    public Geometry getGeometry() {
      return geometry;
    }

    public void setGeometry(Geometry geometry) {
      this.geometry = geometry;
    }
  }

  private static Geometry createGeometry(String wkt) throws ParseException {
    GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
    WKTReader wktReader = new WKTReader(geometryFactory);
    return wktReader.read(wkt);
  }

  @Test
  public void test() throws Exception {
    final String wkt = "POINT (30 10)";
    final Geometry geometry = createGeometry(wkt);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.registerModule(new ShapesAsWKTModule());
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    final GeomWrapper toSerialize = new GeomWrapper();
    toSerialize.setGeometry(geometry);
    String json = objectMapper.writeValueAsString(toSerialize);
    System.out.println(json);
    assertEquals("{\"geometry\":\"POINT (30 10)\"}", json);

    final GeomWrapper deserialized = objectMapper.readValue(json, GeomWrapper.class);
    assertNotNull(deserialized.getGeometry());
  }
}```
@dsmiley
Copy link
Contributor

dsmiley commented Mar 8, 2020

@jdeolive this is curious. Note that the JSON written here has a string value of a point in WKT syntax! (multi-lingual format LOL). I'm not particularly familiar with GeoJSON but that looks like a bug in writing.

I looked at the reading side in a debugger which led me to: org/locationtech/spatial4j/io/jackson/ShapeDeserializer.java:22 which calls a JsonToken.asString. but JsonToken is an enum (immutable); it doesn't have the string it's trying to get. I believe it should call jp.getValueAsString(). Of course this is even if it makes sense to embed WKT inside GeoJSON.

@jdeolive
Copy link
Contributor

jdeolive commented Mar 9, 2020

I guess you could call it valid JSON output but I would say it's definitely invalid GeoJSON. It's also missing a "type" property, required for valid GeoJSON output.

@dsmiley
Copy link
Contributor

dsmiley commented Mar 9, 2020

So then our GeoJson writing has problems?

dsmiley added a commit that referenced this issue Mar 9, 2020
@dsmiley
Copy link
Contributor

dsmiley commented Mar 9, 2020

@jdeolive I put the code here in a branch where you can easily run it.
https://github.com/locationtech/spatial4j/tree/geoJsonTroubles175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants