Java Code Examples for org.locationtech.jts.geom.Geometry#getGeometryType()

The following examples show how to use org.locationtech.jts.geom.Geometry#getGeometryType() . 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: Shape.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Shape geometry2Shape(Geometry geo){
    switch (geo.getGeometryType()){
        case "Point":
        case "MultiPoint":
            if (geo.getNumPoints() < 1)
                return null;
            else
                return new PointShape(geo);
        case "LineString":
        case "MultiLineString":
            if (geo.getNumPoints() < 2)
                return null;
            else
                return new PolylineShape(geo);
        case "Polygon":
        case "MultiPolygon":
            if (geo.getNumPoints() < 3)
                return null;
            else
                return new PolygonShape(geo);
        default:
            return null;
    }
}
 
Example 2
Source File: GeoJSONEncoder.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
protected ObjectNode encodeGeometry(Geometry geometry, int parentSrid)
        throws JSONEncodingException {
    Preconditions.checkNotNull(geometry);
    if (geometry.isEmpty()) {
        return null;
    } else if (geometry instanceof Point) {
        return encode((Point) geometry, parentSrid);
    } else if (geometry instanceof LineString) {
        return encode((LineString) geometry, parentSrid);
    } else if (geometry instanceof Polygon) {
        return encode((Polygon) geometry, parentSrid);
    } else if (geometry instanceof MultiPoint) {
        return encode((MultiPoint) geometry, parentSrid);
    } else if (geometry instanceof MultiLineString) {
        return encode((MultiLineString) geometry, parentSrid);
    } else if (geometry instanceof MultiPolygon) {
        return encode((MultiPolygon) geometry, parentSrid);
    } else if (geometry instanceof GeometryCollection) {
        return encode((GeometryCollection) geometry, parentSrid);
    } else {
        throw new JSONEncodingException("unknown geometry type " + geometry.getGeometryType());
    }
}
 
Example 3
Source File: TestVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public void testBuffer() throws Exception {
    String cql = "the_geom=buffer(the_geom, 20.0)";

    SimpleFeatureCollection testFC = HMTestMaps.getTestFC();
    OmsVectorReshaper reshaper = new OmsVectorReshaper();
    reshaper.inVector = testFC;
    reshaper.pCql = cql;
    reshaper.process();
    SimpleFeatureCollection outFC = reshaper.outVector;
    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature feature = featureIterator.next();
    Geometry geometry = (Geometry) feature.getDefaultGeometry();
    String geometryType = geometry.getGeometryType();
    assertTrue(geometryType.toUpperCase().equals("POLYGON"));
    featureIterator.close();
}
 
Example 4
Source File: TestVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public void testCentroid() throws Exception {
    String cql = "the_geom=centroid(the_geom)";

    SimpleFeatureCollection testFC = HMTestMaps.getTestLeftFC();
    OmsVectorReshaper reshaper = new OmsVectorReshaper();
    reshaper.inVector = testFC;
    reshaper.pCql = cql;
    reshaper.process();
    SimpleFeatureCollection outFC = reshaper.outVector;
    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature feature = featureIterator.next();
    Geometry geometry = (Geometry) feature.getDefaultGeometry();
    String geometryType = geometry.getGeometryType();
    assertTrue(geometryType.toUpperCase().equals("POINT"));
    featureIterator.close();
}
 
Example 5
Source File: JtsGeometrySerde.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void writeGeometry(Geometry geometry, DynamicSliceOutput output)
{
    switch (geometry.getGeometryType()) {
        case "Point":
            writePoint((Point) geometry, output);
            break;
        case "MultiPoint":
            writeMultiPoint((MultiPoint) geometry, output);
            break;
        case "LineString":
            writePolyline(geometry, output, false);
            break;
        case "MultiLineString":
            writePolyline(geometry, output, true);
            break;
        case "Polygon":
            writePolygon(geometry, output, false);
            break;
        case "MultiPolygon":
            writePolygon(geometry, output, true);
            break;
        case "GeometryCollection":
            writeGeometryCollection(geometry, output);
            break;
        default:
            throw new IllegalArgumentException("Unsupported geometry type : " + geometry.getGeometryType());
    }
}
 
Example 6
Source File: PolylineShape.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param geometry Geometry
 */
public PolylineShape(Geometry geometry) {
    this();
    Coordinate[] cs = geometry.getCoordinates();
    List<PointD> points = new ArrayList();
    for (Coordinate c : cs) {
        points.add(new PointD(c.x, c.y));
    }
    switch (geometry.getGeometryType()) {
        case "MultiLineString":
            this._points = points;
            List<PointD> pp;
            int n = geometry.getNumGeometries();
            _numParts = n;
            List<Integer> partlist = new ArrayList<>();
            int idx = 0;
            for (int i = 0; i < n; i++) {
                LineString poly = (LineString) geometry.getGeometryN(i);
                partlist.add(idx);
                Polyline polyline = new Polyline();
                pp = new ArrayList<>();
                for (int j = idx; j < idx + poly.getNumPoints(); j++) {
                    pp.add(points.get(j));
                }
                polyline.setPointList(pp);
                idx += poly.getNumPoints();
                ((List<Polyline>) this._polylines).add(polyline);
            }
            parts = new int[n];
            for (int i = 0; i < parts.length; i++) {
                parts[i] = partlist.get(i);
            }
            this.setExtent(MIMath.getPointsExtent(_points));
            break;
        default:
            this.setPoints(points);
            break;
    }
}
 
Example 7
Source File: ShapeFactory.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create shape from geometry
 * @param a The geometry
 * @return Shape
 */
public static Shape createShape(Geometry a){
    switch (a.getGeometryType()){
        case "Point":
        case "MultiPoint":
            return new PointShape(a);
        case "LineString":
        case "MultiLineString":
            return new PolylineShape(a);
        case "Polygon":
        case "MultiPolygon":
            return new PolygonShape(a);                
    }
    return null;
}
 
Example 8
Source File: GmlEncoderv321.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private String getGmlID(Geometry geom, String gmlId) {
    String id;
    if (!Strings.isNullOrEmpty(gmlId)) {
        id = gmlId;
    } else if (geom.getUserData() != null && geom.getUserData() instanceof Map
            && ((Map) geom.getUserData()).containsKey(XmlBeansEncodingFlags.GMLID.name())) {
        id = (String) ((Map) geom.getUserData()).get(XmlBeansEncodingFlags.GMLID.name());
    } else {
        id = IdGenerator.generate(geom.toText());
    }
    return geom.getGeometryType() + "_" + id;
}
 
Example 9
Source File: TestVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public void testConvexHull() throws Exception {
    String cql = "the_geom=convexHull(the_geom)";

    GeometryFactory gf = GeometryUtilities.gf();
    MultiPoint multiPoint = gf.createMultiPoint(new Coordinate[]{//
            HMTestMaps.getWestNorth(), HMTestMaps.getEastSouth(),
            HMTestMaps.getEastNorth()});
    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
    b.setName("test");
    b.setCRS(HMTestMaps.getCrs());
    b.add("the_geom", MultiPoint.class);
    SimpleFeatureType type = b.buildFeatureType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
    Object[] values = new Object[]{multiPoint};
    builder.addAll(values);
    SimpleFeature feature = builder.buildFeature(null);
    DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
    newCollection.add(feature);

    OmsVectorReshaper reshaper = new OmsVectorReshaper();
    reshaper.inVector = newCollection;
    reshaper.pCql = cql;
    reshaper.process();
    SimpleFeatureCollection outFC = reshaper.outVector;
    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature newFeature = featureIterator.next();
    Geometry geometry = (Geometry) newFeature.getDefaultGeometry();
    String geometryType = geometry.getGeometryType();
    assertTrue(geometryType.toUpperCase().equals("POLYGON"));
    featureIterator.close();
}
 
Example 10
Source File: FeatureUtilities.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw a geometry on a canvas.
 *
 * @param geom                the {@link Geometry} to draw.
 * @param canvas              the {@link Canvas}.
 * @param shapeWriter         the shape writer.
 * @param geometryPaintFill   the fill.
 * @param geometryPaintStroke the stroke.
 */
public static void drawGeometry(Geometry geom, Canvas canvas, ShapeWriter shapeWriter, Paint geometryPaintFill,
                                Paint geometryPaintStroke) {
    String geometryTypeStr = geom.getGeometryType();
    EGeometryType geometryType = EGeometryType.forTypeName(geometryTypeStr);
    DrawableShape shape = shapeWriter.toShape(geom);
    switch (geometryType) {
        case MULTIPOINT:
        case POINT: {
            if (geometryPaintFill != null)
                shape.fill(canvas, geometryPaintFill);
            if (geometryPaintStroke != null)
                shape.draw(canvas, geometryPaintStroke);
        }
        break;
        case MULTILINESTRING:
        case LINESTRING: {
            if (geometryPaintStroke != null)
                shape.draw(canvas, geometryPaintStroke);
        }
        break;
        case MULTIPOLYGON:
        case POLYGON: {
            if (geometryPaintFill != null)
                shape.fill(canvas, geometryPaintFill);
            if (geometryPaintStroke != null)
                shape.draw(canvas, geometryPaintStroke);
        }
        break;
        default:
            break;
    }
}