com.spatial4j.core.context.jts.JtsSpatialContext Java Examples

The following examples show how to use com.spatial4j.core.context.jts.JtsSpatialContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoJSONUtils.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static Map<String, Object> shape2Map(Shape shape) {
    if (shape instanceof ShapeCollection) {
        ShapeCollection<?> shapeCollection = (ShapeCollection<?>)shape;
        List<Map<String, Object>> geometries = new ArrayList<>(shapeCollection.size());
        for(Shape collShape : shapeCollection) {
            geometries.add(shape2Map(collShape));
        }
        return ImmutableMap.of(
                TYPE_FIELD, GEOMETRY_COLLECTION,
                GEOMETRIES_FIELD, geometries
                );
    } else {
        try {
            return GEOJSON_CONVERTER.convert(JtsSpatialContext.GEO.getGeometryFrom(shape));
        } catch (InvalidShapeException e) {
            throw new IllegalArgumentException(
                    String.format(Locale.ENGLISH, "Cannot convert shape %s to Map", shape), e);
        }
    }
}
 
Example #2
Source File: GeoJSONUtils.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private static void validateCoordinate(Object coordinate) {
    try {
        double x;
        double y;
        if (coordinate.getClass().isArray()) {
            Preconditions.checkArgument(Array.getLength(coordinate) == 2, invalidGeoJSON("invalid coordinate"));
            x = ((Number)Array.get(coordinate, 0)).doubleValue();
            y = ((Number)Array.get(coordinate, 1)).doubleValue();
        } else if (coordinate instanceof Collection) {
            Preconditions.checkArgument(((Collection) coordinate).size() == 2, invalidGeoJSON("invalid coordinate"));
            Iterator iter = ((Collection) coordinate).iterator();
            x = ((Number)iter.next()).doubleValue();
            y = ((Number)iter.next()).doubleValue();
        } else {
            throw new IllegalArgumentException(invalidGeoJSON("invalid coordinate"));
        }
        JtsSpatialContext.GEO.verifyX(x);
        JtsSpatialContext.GEO.verifyY(y);
    } catch (InvalidShapeException|ClassCastException e) {
        throw new IllegalArgumentException(invalidGeoJSON("invalid coordinate"), e);
    }
}
 
Example #3
Source File: GeoJSONUtils.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Shape wkt2Shape(String wkt) {
    try {
        return JtsSpatialContext.GEO.readShapeFromWkt(wkt);
    } catch (Throwable e) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                "Cannot convert WKT \"%s\" to shape", wkt), e);
    }
}
 
Example #4
Source File: GeoShapeType.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public int compareValueTo(Map<String, Object> val1, Map<String, Object> val2) {
    // TODO: compare without converting to shape
    Shape shape1 = GeoJSONUtils.map2Shape(val1);
    Shape shape2 = GeoJSONUtils.map2Shape(val2);
    switch (shape1.relate(shape2)) {
        case WITHIN:
            return -1;
        case CONTAINS:
            return 1;
        default:
            return Double.compare(shape1.getArea(JtsSpatialContext.GEO), shape2.getArea(JtsSpatialContext.GEO));
    }
}
 
Example #5
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Query getQuery(Function inner, Context context) {
    RefLiteralPair innerPair = new RefLiteralPair(inner);
    if (!innerPair.isValid()) {
        return null;
    }
    if (innerPair.reference().valueType().equals(DataTypes.GEO_SHAPE)) {
        // we have within('POINT(0 0)', shape_column)
        return genericFunctionFilter(inner, context);
    }
    GeoPointFieldMapper.GeoPointFieldType geoPointFieldType = getGeoPointFieldType(
            innerPair.reference().ident().columnIdent().fqn(),
            context.mapperService);

    Map<String, Object> geoJSON = (Map<String, Object>) innerPair.input().value();
    Shape shape = GeoJSONUtils.map2Shape(geoJSON);
    Geometry geometry = JtsSpatialContext.GEO.getGeometryFrom(shape);
    IndexGeoPointFieldData fieldData = context.fieldDataService.getForField(geoPointFieldType);
    if (geometry.isRectangle()) {
        Rectangle boundingBox = shape.getBoundingBox();
        return new InMemoryGeoBoundingBoxQuery(
                new GeoPoint(boundingBox.getMaxY(), boundingBox.getMinX()),
                new GeoPoint(boundingBox.getMinY(), boundingBox.getMaxX()),
                fieldData
        );
    } else {
        Coordinate[] coordinates = geometry.getCoordinates();
        GeoPoint[] points = new GeoPoint[coordinates.length];
        for (int i = 0; i < coordinates.length; i++) {
            Coordinate coordinate = coordinates[i];
            points[i] = new GeoPoint(coordinate.y, coordinate.x);
        }
        return new GeoPolygonQuery(fieldData, points);
    }
}
 
Example #6
Source File: OPolygonShapeFactory.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
@Override
public Shape makeShape(OCompositeKey key, SpatialContext ctx) {

  SpatialContext ctx1 = JtsSpatialContext.GEO;
  String value = key.getKeys().get(0).toString();

  try {
    return ctx1.getWktShapeParser().parse(value);
  } catch (ParseException e) {
    OLogManager.instance().error(this, "Error on making shape", e);
  }
  return null;
}
 
Example #7
Source File: JTSFootprintParser.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Cut given polygon on poles (89 and -89)
 */
private static String cutOnPoles(String polygonWKT) throws Exception
{
   JtsSpatialContextFactory noCheckFactory = new JtsSpatialContextFactory();
   noCheckFactory.datelineRule = DatelineRule.none;
   noCheckFactory.validationRule = ValidationRule.none;
   JtsSpatialContext noCheckContext = noCheckFactory.newSpatialContext();
   JtsWKTReaderShapeParser noCheckParser =
         new JtsWKTReaderShapeParser(noCheckContext, noCheckFactory);

   JtsGeometry polygon = (JtsGeometry) noCheckParser.parse(polygonWKT);
   JtsGeometry northPole =
         (JtsGeometry) noCheckParser.parse("LINESTRING(180 89, 0 89, -180 89)");
   JtsGeometry southPole =
         (JtsGeometry) noCheckParser.parse("LINESTRING(180 -89, 0 -89, -180 -89)");

   LineMerger lm = new LineMerger();
   lm.add(polygon.getGeom());
   lm.add(northPole.getGeom());
   lm.add(southPole.getGeom());

   Geometry geometry = UnaryUnionOp.union(lm.getMergedLineStrings());

   Polygonizer polygonizer = new Polygonizer();
   polygonizer.add(geometry);

   List<Polygon> foundPolygons = (List<Polygon>) polygonizer.getPolygons();
   List<Polygon> filteredPolygons = new ArrayList<>();
   for (Polygon p: foundPolygons)
   {
      // removing polygons over the poles
      if (p.getCentroid().getCoordinate().y < 89 && p.getCentroid().getCoordinate().y > -89)
      {
         filteredPolygons.add(p);
      }
   }

   Geometry res = null;

   if (!filteredPolygons.isEmpty())
   {
      res = filteredPolygons.get(0);
   }
   if (filteredPolygons.size() > 1)
   {
      // Should not happen...
      LOGGER.error("A Multipolygon was found, instead of a single polygon. Only the first one is retained.");
   }

   WKTWriter wkw = new WKTWriter();
   return wkw.write(res);
}
 
Example #8
Source File: SpatialSupportInitializer.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
protected SpatialContext createSpatialContext() {
    return JtsSpatialContext.GEO;
}
 
Example #9
Source File: SpatialSupportInitializer.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
protected SpatialAlgebra createSpatialAlgebra() {
    return new JtsSpatialAlgebra(JtsSpatialContext.GEO);
}
 
Example #10
Source File: SpatialSupportInitializer.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
protected WktWriter createWktWriter() {
    return new JtsWktWriter(JtsSpatialContext.GEO);
}
 
Example #11
Source File: SpatialSupportInitializer.java    From rya with Apache License 2.0 4 votes vote down vote up
public JtsSpatialAlgebra(JtsSpatialContext context) {
    this.context = context;
}
 
Example #12
Source File: SpatialSupportInitializer.java    From rya with Apache License 2.0 4 votes vote down vote up
public JtsWktWriter(JtsSpatialContext context) {
    this.context = context;
}