All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.locationtech.spatial4j.io.jackson.ShapeDeserializer Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2015 VoyagerSearch and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0 which
 * accompanies this distribution and is available at
 *    http://www.apache.org/licenses/LICENSE-2.0.txt
 ******************************************************************************/

package org.locationtech.spatial4j.io.jackson;

import java.io.IOException;
import org.locationtech.spatial4j.context.SpatialContext;
import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
import org.locationtech.spatial4j.distance.DistanceUtils;
import org.locationtech.spatial4j.shape.Point;
import org.locationtech.spatial4j.shape.Shape;
import org.locationtech.spatial4j.shape.ShapeFactory;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class ShapeDeserializer extends JsonDeserializer
{
  public final SpatialContext ctx;

  public ShapeDeserializer() {
    this(JtsSpatialContext.GEO);
  }
  
  public ShapeDeserializer(SpatialContext ctx) {
    this.ctx = ctx;
  }
  
  public Point readPoint(ArrayNode arr, ShapeFactory factory) {
    double x = arr.get(0).asDouble();
    double y = arr.get(1).asDouble();
    if(arr.size()==3) {
      double z = arr.get(3).asDouble();
      return factory.pointXYZ(x, y, z);
    }
    return factory.pointXY(x, y);
  }
  
  private void fillPoints( ShapeFactory.PointsBuilder b, ArrayNode arrs ) {
    for(int i=0; i b = factory.multiShape(Shape.class);
      ArrayNode arr = (ArrayNode)node.get("geometries");
      for(int i=0; i0) {
        try {
          return ctx.getFormats().read(txt);
        } catch (Exception e) {
          throw new JsonParseException(jp, "error reading shape", e);
        }
      }
      return null; // empty string
    }
    throw new JsonParseException(jp, "can't read GeoJSON yet");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy