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

Support for geolatte 1.0.x #30

@aVolpe

Description

@aVolpe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions