自定义Jackson反序列化Geometry类

Jackson自定义反序列化Geometry类

public class ToJsonGeometryDeserializer<T extends Geometry> extends JsonDeserializer<T> {
    private final GeometryFactory geometryFactory = new GeometryFactory();

    @Override
    public T deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);

        if (node.has("type")) {
            return getGeometry(node);
        }

        throw BizException.newInstance("Invalid Geometry ");
    }

    private T getGeometry(JsonNode node) {
        String type = node.get("type").asText();
        if ("GeometryCollection".equals(type)) {
            JsonNode arrNode = node.get("geometries");
            if (!arrNode.isArray()) {
                throw BizException.newInstance("geometries格式错误");
            }

            Geometry[] geometries = new Geometry[arrNode.size()];
            for (int i = 0, len = arrNode.size(); i < len; i++) {
                geometries[i] = getGeometry(arrNode.get(i));
            }

            return (T) geometryFactory.createGeometryCollection(geometries);
        }
        JsonNode arrNode = node.get("coordinates");
        if ("Point".equals(type)) {
            return (T) getPoint(geometryFactory, arrNode);
        } else if ("MultiPoint".equals(type)) {
            if (!arrNode.isArray() || arrNode.size() < 2) {
                throw BizException.newInstance("coordinates格式错误");
            }

            Point[] points = new Point[arrNode.size()];
            for (int i = 0, len = arrNode.size(); i < len; i++) {
                points[i] = getPoint(geometryFactory, arrNode.get(i));
            }

            return (T) geometryFactory.createMultiPoint(points);
        } else if ("LineString".equals(type)) {
            return (T) getLineString(geometryFactory, arrNode);
        } else if ("MultiLineString".equals(type)) {
            if (!arrNode.isArray() || arrNode.size() < 2) {
                throw BizException.newInstance("coordinates格式错误");
            }
            LineString[] lineStrings = new LineString[arrNode.size()];
            for (int i = 0, len = arrNode.size(); i < len; i++) {
                lineStrings[i] = getLineString(geometryFactory, arrNode.get(i));
            }
            return (T) geometryFactory.createMultiLineString(lineStrings);
        } else if ("Polygon".equals(type)) {
            if (!arrNode.isArray()) {
                throw BizException.newInstance("coordinates格式错误");
            }

            return (T) getPolygon(geometryFactory, arrNode);
        } else if ("MultiPolygon".equals(type)) {
            if (!arrNode.isArray()) {
                throw BizException.newInstance("coordinates格式错误");
            }

            Polygon[] polygons = new Polygon[arrNode.size()];
            for (int i = 0, len = arrNode.size(); i < len; i++) {
                polygons[i] = getPolygon(geometryFactory, arrNode.get(i));
            }
            return (T) geometryFactory.createMultiPolygon(polygons);
        } else {
            throw BizException.newInstance("Invalid Geometry Type " + type);
        }
    }

    private Polygon getPolygon(GeometryFactory geometryFactory, JsonNode arrNode) {
        LinearRing shell = getLineRing(geometryFactory, arrNode.get(0));

        LinearRing[] holes = new LinearRing[arrNode.size() - 1];
        for (int i = 1, len = arrNode.size(); i < len; i++) {
            holes[i - 1] = getLineRing(geometryFactory, arrNode.get(i));
        }
        return geometryFactory.createPolygon(shell, holes);
    }

    private Point getPoint(GeometryFactory geometryFactory, JsonNode arrNode) {
        Coordinate coordinate = getPointCoordinate(arrNode);
        return geometryFactory.createPoint(coordinate);
    }

    private LineString getLineString(GeometryFactory geometryFactory, JsonNode arrNode) {
        Coordinate[] coordinates = getLineCoordinates(arrNode);
        return geometryFactory.createLineString(coordinates);
    }

    private LinearRing getLineRing(GeometryFactory geometryFactory, JsonNode jsonNode) {
        Coordinate[] coordinates = getLineCoordinates(jsonNode);
        return geometryFactory.createLinearRing(coordinates);
    }

    private Coordinate[] getLineCoordinates(JsonNode jsonNode) {
        if (!jsonNode.isArray() || jsonNode.size() < 2) {
            throw BizException.newInstance("coordinates格式错误");
        }
        Coordinate[] coordinates = new Coordinate[jsonNode.size()];
        for (int i = 0, len = jsonNode.size(); i < len; i++) {
            // 点
            JsonNode arrNode = jsonNode.get(i);
            coordinates[i] = getPointCoordinate(arrNode);
        }
        return coordinates;
    }

    private Coordinate getPointCoordinate(JsonNode arrNode) {
        if (!arrNode.isArray() || arrNode.size() < 1 || arrNode.size() > 2) {
            throw BizException.newInstance("coordinates格式错误");
        }
        double x = arrNode.get(0).asDouble();
        double y = arrNode.get(1).asDouble();
        return new Coordinate(x, y);
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper om = new ObjectMapper();

        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addDeserializer(Geometry.class, new ToJsonGeometryDeserializer());
        om.registerModule(simpleModule);

        String pointJson = "{\n" +
                "    \"type\": \"Point\",\n" +
                "    \"coordinates\": [104.6, 30.1]\n" +
                "}";

        Geometry point = om.readValue(pointJson, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(point));

        String lineJson = "{\n" +
                "    \"type\": \"LineString\",\n" +
                "    \"coordinates\": [\n" +
                "        [\n" +
                "            104.6,\n" +
                "            30.1\n" +
                "        ],\n" +
                "        [\n" +
                "            104.7,\n" +
                "            30.2\n" +
                "        ],\n" +
                "        [\n" +
                "            104.8,\n" +
                "            30.3\n" +
                "        ]\n" +
                "    ]\n" +
                "}";

        Geometry line = om.readValue(lineJson, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(line));

        String polygonJson = "{\n" +
                "    \"type\": \"Polygon\",\n" +
                "    \"coordinates\": [\n" +
                "        [\n" +
                "            [104.6, 30.1],\n" +
                "            [104.7, 30.2],\n" +
                "            [104.8, 30.3],\n" +
                "            [104.6, 30.1]\n" +
                "        ]\n" +
                "    ]\n" +
                "}";

        Geometry polygon = om.readValue(polygonJson, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(polygon));

        String polygonJson2 = "{\n" +
                "  \"type\" : \"Polygon\",\n" +
                "  \"coordinates\" : [\n" +
                "     [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],\n" +
                "     [ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]\n" +
                "  ]\n" +
                "}";

        Geometry polygon2 = om.readValue(polygonJson2, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(polygon2));

        String multiPointJson = "{\n" +
                "    \"type\": \"MultiPoint\",\n" +
                "    \"coordinates\": [\n" +
                "        [125.6, 10.1],\n" +
                "        [125.7, 10.2],\n" +
                "        [125.8, 10.3]\n" +
                "    ]\n" +
                "}";

        Geometry multiPoint = om.readValue(multiPointJson, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(multiPoint));

        String multiLineStringJson = "{\n" +
                "    \"type\": \"MultiLineString\",\n" +
                "    \"coordinates\": [\n" +
                "        [\n" +
                "            [125.6, 10.1],\n" +
                "            [126.0, 11.0],\n" +
                "            [126.4, 12.0]\n" +
                "        ],\n" +
                "        [\n" +
                "            [125.6, 10.1],\n" +
                "            [125.7, 10.2],\n" +
                "            [125.8, 10.3]\n" +
                "        ]\n" +
                "    ]\n" +
                "}";

        Geometry multiLineString = om.readValue(multiLineStringJson, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(multiLineString));

        String multiPolygonJson = "{\n" +
                "    \"type\": \"MultiPolygon\",\n" +
                "    \"coordinates\": [\n" +
                "        [\n" +
                "            [\n" +
                "                [102.0, 2.0],\n" +
                "                [103.0, 2.0],\n" +
                "                [103.0, 3.0],\n" +
                "                [102.0, 3.0],\n" +
                "                [102.0, 2.0]\n" +
                "            ]\n" +
                "        ],\n" +
                "        [\n" +
                "            [\n" +
                "                [100.0, 0.0],\n" +
                "                [101.0, 0.0],\n" +
                "                [101.0, 1.0],\n" +
                "                [100.0, 1.0],\n" +
                "                [100.0, 0.0]\n" +
                "            ],\n" +
                "            [\n" +
                "                [100.2, 0.2],\n" +
                "                [100.8, 0.2],\n" +
                "                [100.8, 0.8],\n" +
                "                [100.2, 0.8],\n" +
                "                [100.2, 0.2]\n" +
                "            ]\n" +
                "        ]\n" +
                "    ]\n" +
                "}";

        Geometry multiPolygon = om.readValue(multiPolygonJson, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(multiPolygon));


        String geometryCollectionJson = "{\n" +
                "    \"type\": \"GeometryCollection\",\n" +
                "    \"geometries\": [\n" +
                "        {\n" +
                "            \"type\": \"Point\",\n" +
                "            \"coordinates\": [125.6, 10.1]\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"LineString\",\n" +
                "            \"coordinates\": [\n" +
                "                [125.6, 10.1],\n" +
                "                [126.0, 11.0],\n" +
                "                [126.4, 12.0]\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Polygon\",\n" +
                "            \"coordinates\": [\n" +
                "                [\n" +
                "                    [100.0, 0.0],\n" +
                "                    [101.0, 0.0],\n" +
                "                    [101.0, 1.0],\n" +
                "                    [100.0, 1.0],\n" +
                "                    [100.0, 0.0]\n" +
                "                ]\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        Geometry geometryCollection = om.readValue(geometryCollectionJson, Geometry.class);
        System.out.println(ConverterGeoJson.convertToGeoJson(geometryCollection));
    }
}

运行测试结果

{"type":"Point","coordinates":[104.6,30.1]}
{"type":"LineString","coordinates":[[104.6,30.1],[104.7,30.2],[104.8,30.3]]}
{"type":"Polygon","coordinates":[[[104.6,30.1],[104.7,30.2],[104.8,30.3],[104.6,30.1]]]}
{"type":"Polygon","coordinates":[[[0.0,0.0],[3,6],[6,1],[0.0,0.0]],[[2,2],[3,3],[4,2],[2,2]]]}
{"type":"MultiPoint","coordinates":[[125.6,10.1],[125.7,10.2],[125.8,10.3]]}
{"type":"MultiLineString","coordinates":[[[125.6,10.1],[126,11],[126.4,12]],[[125.6,10.1],[125.7,10.2],[125.8,10.3]]]}
{"type":"MultiPolygon","coordinates":[[[[102,2],[103,2],[103,3],[102,3],[102,2]]],[[[100,0.0],[101,0.0],[101,1],[100,1],[100,0.0]],[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]]}
{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[125.6,10.1]},{"type":"LineString","coordinates":[[125.6,10.1],[126,11],[126.4,12]]},{"type":"Polygon","coordinates":[[[100,0.0],[101,0.0],[101,1],[100,1],[100,0.0]]]}]}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值