Java Code Examples for org.locationtech.jts.geom.GeometryCollection#getNumGeometries()

The following examples show how to use org.locationtech.jts.geom.GeometryCollection#getNumGeometries() . 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: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
private static void buildPointsBlock(Geometry geometry, BlockBuilder blockBuilder)
{
    GeometryType type = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
    if (type == GeometryType.POINT) {
        GEOMETRY.writeSlice(blockBuilder, JtsGeometrySerde.serialize(geometry));
    }
    else if (type == GeometryType.GEOMETRY_COLLECTION) {
        GeometryCollection collection = (GeometryCollection) geometry;
        for (int i = 0; i < collection.getNumGeometries(); i++) {
            Geometry entry = collection.getGeometryN(i);
            buildPointsBlock(entry, blockBuilder);
        }
    }
    else {
        GeometryFactory geometryFactory = geometry.getFactory();
        Coordinate[] vertices = geometry.getCoordinates();
        for (Coordinate coordinate : vertices) {
            GEOMETRY.writeSlice(blockBuilder, JtsGeometrySerde.serialize(geometryFactory.createPoint(coordinate)));
        }
    }
}
 
Example 2
Source File: GeoJSONEncoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
public ObjectNode encode(GeometryCollection geometry, int parentSrid)
        throws JSONEncodingException {
    Preconditions.checkNotNull(geometry);
    ObjectNode json = jsonFactory.objectNode();
    ArrayNode geometries =
            json.put(JSONConstants.TYPE, JSONConstants.GEOMETRY_COLLECTION).putArray(JSONConstants.GEOMETRIES);
    int srid = encodeCRS(json, geometry, parentSrid);
    for (int i = 0; i < geometry.getNumGeometries(); ++i) {
        geometries.add(encodeGeometry(geometry.getGeometryN(i), srid));
    }
    return json;
}
 
Example 3
Source File: SpatialiteWKBWriter.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private void writeGeometryCollection( int geometryType, GeometryCollection gc, OutStream os ) throws IOException {
    // writeByteOrder(os);
    writeGeometryType(geometryType, gc, os);
    writeInt(gc.getNumGeometries(), os);
    for( int i = 0; i < gc.getNumGeometries(); i++ ) {
        write(gc.getGeometryN(i), os);
    }
}
 
Example 4
Source File: HistogramStatistics.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void entryIngested(final GridCoverage entry, final GeoWaveRow... geoWaveRows) {
  /*
   * Create the operation for the Histogram with a ROI. No subsampling should be applied.
   */
  final Geometry footprint;
  if (entry instanceof FitToIndexGridCoverage) {
    footprint = ((FitToIndexGridCoverage) entry).getFootprintWorldGeometry();
    if (footprint == null) {
      return;
    }
  } else {
    // this is a condition that isn't going to be exercised typically in
    // any code, but at this point we will assume default CRS
    footprint = RasterUtils.getFootprint(entry, GeoWaveGTRasterFormat.DEFAULT_CRS);
  }

  final GridCoverage originalCoverage;
  Resolution resolution = null;
  if (entry instanceof FitToIndexGridCoverage) {
    originalCoverage = ((FitToIndexGridCoverage) entry).getOriginalCoverage();
    resolution = ((FitToIndexGridCoverage) entry).getResolution();
  } else {
    originalCoverage = entry;
  }
  if (footprint instanceof GeometryCollection) {
    final GeometryCollection collection = (GeometryCollection) footprint;
    for (int g = 0; g < collection.getNumGeometries(); g++) {
      final Geometry geom = collection.getGeometryN(g);
      if (geom instanceof Polygon) {
        mergePoly(originalCoverage, (Polygon) geom, resolution);
      }
    }
  } else if (footprint instanceof Polygon) {
    mergePoly(originalCoverage, (Polygon) footprint, resolution);
  }
}
 
Example 5
Source File: TWKBWriter.java    From geowave with Apache License 2.0 5 votes vote down vote up
private void writeGeometryCollection(
    final GeometryCollection geoms,
    final PrecisionWriter precision,
    final DataOutput output) throws IOException {
  Varint.writeUnsignedVarInt(geoms.getNumGeometries(), output);
  for (int i = 0; i < geoms.getNumGeometries(); i++) {
    final Geometry geom = geoms.getGeometryN(i);
    write(geom, output);
  }
}
 
Example 6
Source File: GeoJSONUtils.java    From crate with Apache License 2.0 5 votes vote down vote up
public Map<String, Object> convert(Geometry geometry) {
    HashMap<String, Object> builder = new HashMap<>();

    if (geometry instanceof Point) {
        builder.put(TYPE_FIELD, POINT);
        builder.put(COORDINATES_FIELD, extract((Point) geometry));
    } else if (geometry instanceof MultiPoint) {
        builder.put(TYPE_FIELD, MULTI_POINT);
        builder.put(COORDINATES_FIELD, extract((MultiPoint) geometry));
    } else if (geometry instanceof LineString) {
        builder.put(TYPE_FIELD, LINE_STRING);
        builder.put(COORDINATES_FIELD, extract((LineString) geometry));
    } else if (geometry instanceof MultiLineString) {
        builder.put(TYPE_FIELD, MULTI_LINE_STRING);
        builder.put(COORDINATES_FIELD, extract((MultiLineString) geometry));
    } else if (geometry instanceof Polygon) {
        builder.put(TYPE_FIELD, POLYGON);
        builder.put(COORDINATES_FIELD, extract((Polygon) geometry));
    } else if (geometry instanceof MultiPolygon) {
        builder.put(TYPE_FIELD, MULTI_POLYGON);
        builder.put(COORDINATES_FIELD, extract((MultiPolygon) geometry));
    } else if (geometry instanceof GeometryCollection) {
        GeometryCollection geometryCollection = (GeometryCollection) geometry;
        int size = geometryCollection.getNumGeometries();
        List<Map<String, Object>> geometries = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            geometries.add(convert(geometryCollection.getGeometryN(i)));
        }
        builder.put(TYPE_FIELD, GEOMETRY_COLLECTION);
        builder.put(GEOMETRIES_FIELD, geometries);
    } else {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "Cannot extract coordinates from geometry %s", geometry.getGeometryType()));
    }
    return Collections.unmodifiableMap(builder);
}
 
Example 7
Source File: ShapeWriter.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private DrawableShape toShape(GeometryCollection gc) {
    GeometryCollectionShape shape = new GeometryCollectionShape();
    // add components to GC shape
    for (int i = 0; i < gc.getNumGeometries(); i++) {
        Geometry g = (Geometry) gc.getGeometryN(i);
        shape.add(toShape(g));
    }
    return shape;
}
 
Example 8
Source File: JtsGeometryCollectionIterable.java    From geogson with Apache License 2.0 5 votes vote down vote up
public static JtsGeometryCollectionIterable of(final GeometryCollection src) {
    return new JtsGeometryCollectionIterable(new GeometryProvider() {
        @Override
        public int getNumGeometries() {
            return src.getNumGeometries();
        }

        @Override
        public Geometry getGeometryN(int n) {
            return src.getGeometryN(n);
        }
    });
}
 
Example 9
Source File: GeoUtils.java    From elasticsearch-plugin-geoshape with MIT License 3 votes vote down vote up
public static GeometryCollection removeDuplicateCoordinates(GeometryCollection geometryCollection) {

        Geometry[] geometries = new Geometry[geometryCollection.getNumGeometries()];

        for (int i = 0; i < geometryCollection.getNumGeometries(); i++) {
            geometries[i] = removeDuplicateCoordinates(geometryCollection.getGeometryN(i));
        }

        return geometryCollection.getFactory().createGeometryCollection(geometries);
    }
 
Example 10
Source File: GeometryTransform.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Transforms the given geometries. Can be invoked directly if the type is known at compile-time,
 * or indirectly through a call to the more generic {@link #transform(Geometry)} method.
 *
 * @param  geom  the geometries to transform.
 * @return the transformed geometries.
 * @throws TransformException if an error occurred while transforming a geometry.
 */
public GeometryCollection transform(final GeometryCollection geom) throws TransformException {
    final Geometry[] subs = new Geometry[geom.getNumGeometries()];
    for (int i = 0; i < subs.length; i++) {
        subs[i] = transform(geom.getGeometryN(i));
    }
    return geometryFactory.createGeometryCollection(subs);
}