Skip to content
This repository has been archived by the owner on Oct 8, 2022. It is now read-only.

Support for geolatte 1.0.x #30

Open
aVolpe opened this issue Aug 11, 2016 · 3 comments
Open

Support for geolatte 1.0.x #30

aVolpe opened this issue Aug 11, 2016 · 3 comments

Comments

@aVolpe
Copy link

aVolpe commented Aug 11, 2016

The current code doesn't work well with geolatte 1.0.x, I don't know how many changes are required to make it works, but I will try to add a bit.

This is a PolygonSerializer that works well with the current jackson.

import java.io.IOException;

import org.geolatte.common.dataformats.json.jackson.JsonMapper;
import org.geolatte.common.dataformats.json.jackson.PolygonSerializer;
import org.geolatte.geom.C2D;
import org.geolatte.geom.Envelope;
import org.geolatte.geom.LineString;
import org.geolatte.geom.Polygon;
import org.geolatte.geom.Position;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class PolygonToGeoJSON extends PolygonSerializer {

    public PolygonToGeoJSON() {
        super(new JsonMapper());
    }

    @Override
    @SuppressWarnings("rawtypes")
    protected void writeShapeSpecificSerialization(Polygon value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException {

        jgen.writeFieldName("type");
        jgen.writeString("Polygon");
        jgen.writeArrayFieldStart("coordinates");
        JsonSerializer<Object> ser = provider.findValueSerializer(Double.class, null);
        writeLineString(value.getExteriorRing(), jgen, provider, ser);
        for (int i = 0; i < value.getNumInteriorRing(); i++) {
            writeLineString(value.getInteriorRingN(i), jgen, provider, ser);
        }

        jgen.writeEndArray();
    }

    /**
     * Writes a ring to the json
     */
    protected void writeLineString(LineString<C2D> line, JsonGenerator jgen, SerializerProvider provider,
            JsonSerializer<Object> ser) throws IOException {

        jgen.writeStartArray();
        for (int i = 0; i < line.getNumPositions(); i++) {

            jgen.writeStartArray();
            C2D position = line.getPositionN(i);
            ser.serialize(position.getX(), jgen, provider);
            ser.serialize(position.getY(), jgen, provider);
            jgen.writeEndArray();
        }

        jgen.writeEndArray();
    }

    @Override
    @SuppressWarnings("rawtypes")
    protected double[] envelopeToCoordinates(Envelope e) {

        Position upperLeft = e.upperLeft();
        Position lowerRigth = e.lowerRight();
        return new double[] { lowerRigth.getCoordinate(0), lowerRigth.getCoordinate(1), upperLeft.getCoordinate(0),
                upperLeft.getCoordinate(1) };
    }
}

Also, because I don't know how to change all the geometry types I can't make a decent pull request.

Obs: I am not sure about the envelopeToCoordinates method.

@aVolpe
Copy link
Author

aVolpe commented Aug 11, 2016

This works well with a property like this:

@JsonSerialize(using = PolygonToGeoJSON.class)
Polygon<C2D> shape;

@rivasyafri
Copy link

I know that the current code doesn't work well with geolatte geom 1.0.x. Can you give me insight how to deserialize Polygon? The GeometryDeserialize isn't working

@jwgmeligmeyling
Copy link
Contributor

For 1.x you want to use the GeolatteGeomModule from the org.geolatte:geolatte-geojson artefact.

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

No branches or pull requests

3 participants