Java Code Examples for org.locationtech.jts.geom.MultiLineString

The following examples show how to use org.locationtech.jts.geom.MultiLineString. These examples are extracted from open source projects. 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 Project: arctic-sea   Source File: GeoJSONEncoder.java    License: 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 2
public void write( Geometry geom, OutStream os ) throws IOException {
    buf[0] = 0x69;
    os.write(buf, 1);
    if (geom instanceof Point)
        writePoint((Point) geom, os);
    // LinearRings will be written as LineStrings
    else if (geom instanceof LineString)
        writeLineString((LineString) geom, os);
    else if (geom instanceof Polygon)
        writePolygon((Polygon) geom, os);
    else if (geom instanceof MultiPoint)
        writeGeometryCollection(WKBConstants.wkbMultiPoint, (MultiPoint) geom, os);
    else if (geom instanceof MultiLineString)
        writeGeometryCollection(WKBConstants.wkbMultiLineString, (MultiLineString) geom, os);
    else if (geom instanceof MultiPolygon)
        writeGeometryCollection(WKBConstants.wkbMultiPolygon, (MultiPolygon) geom, os);
    else if (geom instanceof GeometryCollection)
        writeGeometryCollection(WKBConstants.wkbGeometryCollection, (GeometryCollection) geom, os);
    else {
        Assert.shouldNeverReachHere("Unknown Geometry type");
    }
}
 
Example 3
private ArrayList<Geometry> getPolygonBetweenLines( ArrayList<SimpleFeature> newLinesFeatures ) {
    ArrayList<Geometry> polygons = new ArrayList<Geometry>();
    for( int i = 0; i < newLinesFeatures.size() - 1; i++ ) {
        SimpleFeature f1 = newLinesFeatures.get(i);
        SimpleFeature f2 = newLinesFeatures.get(i + 1);

        LineString l1 = (LineString) f1.getDefaultGeometry();
        LineString l2 = (LineString) f2.getDefaultGeometry();
        MultiLineString multiLine = gf.createMultiLineString(new LineString[]{l1, l2});

        Geometry convexHull = multiLine.convexHull();
        Geometry buffer = convexHull.buffer(0.1);
        polygons.add(buffer);
    }
    return polygons;
}
 
Example 4
Source Project: snap-desktop   Source File: ShapefileLoader.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static Style[] createStyle(File shapeFile, FeatureType schema) {
    final Style[] styles = SLDUtils.loadSLD(shapeFile);
    if (styles != null && styles.length > 0) {
        return styles;
    }
    Class<?> type = schema.getGeometryDescriptor().getType().getBinding();
    if (type.isAssignableFrom(Polygon.class)
            || type.isAssignableFrom(MultiPolygon.class)) {
        return new Style[]{createPolygonStyle()};
    } else if (type.isAssignableFrom(LineString.class)
            || type.isAssignableFrom(MultiLineString.class)) {
        return new Style[]{createLineStyle()};
    } else {
        return new Style[]{createPointStyle()};
    }
}
 
Example 5
Source Project: java-vector-tile   Source File: VectorTileEncoder.java    License: Apache License 2.0 6 votes vote down vote up
static VectorTile.Tile.GeomType toGeomType(Geometry geometry) {
    if (geometry instanceof Point) {
        return VectorTile.Tile.GeomType.POINT;
    }
    if (geometry instanceof MultiPoint) {
        return VectorTile.Tile.GeomType.POINT;
    }
    if (geometry instanceof LineString) {
        return VectorTile.Tile.GeomType.LINESTRING;
    }
    if (geometry instanceof MultiLineString) {
        return VectorTile.Tile.GeomType.LINESTRING;
    }
    if (geometry instanceof Polygon) {
        return VectorTile.Tile.GeomType.POLYGON;
    }
    if (geometry instanceof MultiPolygon) {
        return VectorTile.Tile.GeomType.POLYGON;
    }
    return VectorTile.Tile.GeomType.UNKNOWN;
}
 
Example 6
Source Project: geowave   Source File: GeoWaveSpatialEncoders.java    License: Apache License 2.0 6 votes vote down vote up
public static void registerUDTs() {
  UDTRegistration.register(
      Geometry.class.getCanonicalName(),
      GeometryUDT.class.getCanonicalName());
  UDTRegistration.register(Point.class.getCanonicalName(), PointUDT.class.getCanonicalName());
  UDTRegistration.register(
      LineString.class.getCanonicalName(),
      LineStringUDT.class.getCanonicalName());
  UDTRegistration.register(Polygon.class.getCanonicalName(), PolygonUDT.class.getCanonicalName());

  UDTRegistration.register(
      MultiLineString.class.getCanonicalName(),
      MultiLineStringUDT.class.getCanonicalName());
  UDTRegistration.register(
      MultiPoint.class.getCanonicalName(),
      MultiPointUDT.class.getCanonicalName());
  UDTRegistration.register(
      MultiPolygon.class.getCanonicalName(),
      MultiPolygonUDT.class.getCanonicalName());
}
 
Example 7
Source Project: geowave   Source File: TWKBWriter.java    License: Apache License 2.0 6 votes vote down vote up
private byte getType(final Geometry geom) {
  if (geom instanceof Point) {
    return TWKBUtils.POINT_TYPE;
  } else if (geom instanceof LineString) {
    return TWKBUtils.LINESTRING_TYPE;
  } else if (geom instanceof Polygon) {
    return TWKBUtils.POLYGON_TYPE;
  } else if (geom instanceof MultiPoint) {
    return TWKBUtils.MULTIPOINT_TYPE;
  } else if (geom instanceof MultiLineString) {
    return TWKBUtils.MULTILINESTRING_TYPE;
  } else if (geom instanceof MultiPolygon) {
    return TWKBUtils.MULTIPOLYGON_TYPE;
  }
  return TWKBUtils.GEOMETRYCOLLECTION_TYPE;
}
 
Example 8
Source Project: geopaparazzi   Source File: ShapeWriter.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a {@link Shape} representing a {@link Geometry},
 * according to the specified PointTransformation
 * and PointShapeFactory (if relevant).
 * <p>
 * Note that Shapes do not
 * preserve information fragment_about which elements in heterogeneous collections
 * are 1D and which are 2D.
 * For example, a GeometryCollection containing a ring and a
 * disk will render as two disks if Graphics.fill is used,
 * or as two rings if Graphics.draw is used.
 * To avoid this issue use separate shapes for the components.
 *
 * @param geometry the geometry to convert
 * @return a Shape representing the geometry
 */
public DrawableShape toShape(Geometry geometry) {
    if (geometry.isEmpty())
        return new PathShape(new Path());
    else if (geometry instanceof Polygon)
        return toShape((Polygon) geometry);
    else if (geometry instanceof MultiPolygon)
        return toShape((MultiPolygon) geometry);
    else if (geometry instanceof LineString)
        return toShape((LineString) geometry);
    else if (geometry instanceof MultiLineString)
        return toShape((MultiLineString) geometry);
    else if (geometry instanceof Point)
        return toShape((Point) geometry);
    else if (geometry instanceof MultiPoint)
        return toShape((MultiPoint) geometry);
    else if (geometry instanceof GeometryCollection)
        return toShape((GeometryCollection) geometry);

    throw new IllegalArgumentException("Unrecognized Geometry class: " + geometry.getClass());
}
 
Example 9
Source Project: searoute   Source File: SeaRoutingTest.java    License: European Union Public License 1.2 5 votes vote down vote up
private void test(double oLon, double oLat, double dLon, double dLat, boolean allowSuez, boolean allowPanama, double rglMin, double rglMax, double dMin, double dMax) {

		for(int resKM : new int[] {5, 10, 20, 50, 100}) {		
			//create the routing object
			SeaRouting sr = new SeaRouting(resKM);

			//get the route
			Feature route = sr.getRoute(oLon, oLat, dLon, dLat, allowSuez, allowPanama);

			//compute the distance in km
			MultiLineString routeGeom = (MultiLineString) route.getGeometry();
			//if(routeGeom == null) return;
			double d = GeoDistanceUtil.getLengthGeoKM(routeGeom);

			//export the route in geoJSON format
			String rgj = SeaRouting.toGeoJSON(routeGeom);
			assertTrue(rgj.length() > 10);

			assertFalse(route.getAttribute("dToKM") == null);
			assertFalse(route.getAttribute("dFromKM") == null);
			assertFalse(routeGeom == null);
			assertTrue(routeGeom.getLength() < rglMax);
			assertTrue(routeGeom.getLength() > rglMin);
			assertTrue(d < dMax);
			assertTrue(d > dMin);

			//LOGGER.info(route.getAttributes()); //{dToKM=30.434689972094713, dFromKM=4.134285055159354}
			//LOGGER.info(routeGeom.getLength()); //239.80414982436778
			//LOGGER.info(d); //25091.977608633395
			//LOGGER.info(rgj); //{"type":"MultiLineString","coordinates":[[[5.2]]]}
		}
	}
 
Example 10
Source Project: arctic-sea   Source File: JTSHelper.java    License: Apache License 2.0 5 votes vote down vote up
public Geometry convertSequence(Geometry geometry) {
    if (geometry instanceof Point) {
        return createPoint(((Point) geometry).getCoordinateSequence());
    } else if (geometry instanceof LinearRing) {
        return createLinearRing(((LinearRing) geometry).getCoordinates());
    } else if (geometry instanceof LineString) {
        return createLineString(((LineString) geometry).getCoordinates());
    } else if (geometry instanceof Polygon) {
        LinearRing[] linearRings = new LinearRing[((Polygon) geometry).getNumInteriorRing()];
        for (int i = 0; i < ((Polygon) geometry).getNumInteriorRing(); i++) {
            linearRings[i] = (LinearRing) convertSequence(((Polygon) geometry).getInteriorRingN(i));
        }
        return createPolygon((LinearRing) convertSequence(((Polygon) geometry).getExteriorRing()),
                linearRings);
    } else if (geometry instanceof MultiPoint) {
        return createMultiPointFromCoords(((MultiPoint) geometry).getCoordinates());
    } else if (geometry instanceof MultiLineString) {
        LineString[] lineStrings = new LineString[((MultiLineString) geometry).getNumGeometries()];
        for (int i = 0; i < ((MultiLineString) geometry).getNumGeometries(); i++) {
            lineStrings[i] = (LineString) convertSequence(((MultiLineString) geometry).getGeometryN(i));
        }
        return createMultiLineString(lineStrings);
    } else if (geometry instanceof MultiPolygon) {
        Polygon[] polygons = new Polygon[((MultiPolygon) geometry).getNumGeometries()];
        for (int i = 0; i < ((MultiPolygon) geometry).getNumGeometries(); i++) {
            polygons[i] = (Polygon) convertSequence(((MultiPolygon) geometry).getGeometryN(i));
        }
        return createMultiPolygon(polygons);
    } else if (geometry instanceof GeometryCollection) {
        Geometry[] geometries = new Geometry[((GeometryCollection) geometry).getNumGeometries()];
        for (int i = 0; i < ((GeometryCollection) geometry).getNumGeometries(); i++) {
            geometries[i] = convertSequence(((GeometryCollection) geometry).getGeometryN(i));
        }
        return createGeometryCollection(geometries);
    }
    return geometry;
}
 
Example 11
Source Project: arctic-sea   Source File: GeoJSONDecoder.java    License: Apache License 2.0 5 votes vote down vote up
protected MultiLineString decodeMultiLineString(JsonNode node, GeometryFactory fac)
        throws GeoJSONDecodingException {
    JsonNode coordinates = requireCoordinates(node);
    LineString[] lineStrings = new LineString[coordinates.size()];
    for (int i = 0; i < coordinates.size(); ++i) {
        JsonNode coords = coordinates.get(i);
        lineStrings[i] = fac.createLineString(decodeCoordinates(coords));
    }
    return fac.createMultiLineString(lineStrings);
}
 
Example 12
Source Project: arctic-sea   Source File: GeoJSONEncoder.java    License: Apache License 2.0 5 votes vote down vote up
protected ObjectNode encode(MultiLineString geometry, int parentSrid) {
    Preconditions.checkNotNull(geometry);
    ObjectNode json = jsonFactory.objectNode();
    ArrayNode list =
            json.put(JSONConstants.TYPE, JSONConstants.MULTI_LINE_STRING).putArray(JSONConstants.COORDINATES);
    for (int i = 0; i < geometry.getNumGeometries(); ++i) {
        list.add(encodeCoordinates((LineString) geometry.getGeometryN(i)));
    }
    encodeCRS(json, geometry, parentSrid);
    return json;
}
 
Example 13
Source Project: arctic-sea   Source File: GeoJSONTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiLineStringWithZCoordinate() {
    MultiLineString geometry = randomMultiLineString(EPSG_4326);
    geometry.apply(new RandomZCoordinateFilter());
    geometry.geometryChanged();
    readWriteTest(geometry);
}
 
Example 14
Source Project: arctic-sea   Source File: SamplingDecoderv20.java    License: Apache License 2.0 5 votes vote down vote up
private String getFeatTypeForGeometry(final Geometry geometry) {
    if (geometry instanceof Point || geometry instanceof MultiPoint) {
        return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_POINT;
    } else if (geometry instanceof LineString || geometry instanceof MultiLineString) {
        return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_CURVE;
    } else if (geometry instanceof Polygon || geometry instanceof MultiPolygon) {
        return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_SURFACE;
    }
    return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_FEATURE;
}
 
Example 15
Source Project: arctic-sea   Source File: SamplingEncoderv20.java    License: Apache License 2.0 5 votes vote down vote up
private String getFeatureType(Geometry geometry) {
    if (geometry instanceof Point || geometry instanceof MultiPoint) {
        return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_POINT;
    } else if (geometry instanceof LineString || geometry instanceof MultiLineString) {
        return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_CURVE;
    } else if (geometry instanceof Polygon || geometry instanceof MultiPolygon) {
        return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_SURFACE;
    } else {
        return SfConstants.SAMPLING_FEAT_TYPE_SF_SAMPLING_FEATURE;
    }
}
 
Example 16
/** Populate member data. */
private static void populate() {
    geometryMap.put(Point.class, GeometryTypeEnum.POINT);
    geometryMap.put(MultiPoint.class, GeometryTypeEnum.POINT);
    geometryMap.put(LineString.class, GeometryTypeEnum.LINE);
    geometryMap.put(MultiLineString.class, GeometryTypeEnum.LINE);
    geometryMap.put(Polygon.class, GeometryTypeEnum.POLYGON);
    geometryMap.put(MultiPolygon.class, GeometryTypeEnum.POLYGON);
    geometryMap.put(GridCoverage2D.class, GeometryTypeEnum.RASTER);
}
 
Example 17
public MultiLineString createRandomMultiLineString() {
    LineString[] lineStrings = new LineString[numGeometries];
    for (int i=0; i<numGeometries; i++) {
        lineStrings[i] = createRandomLineString();
    }
    return geometryFactory.createMultiLineString(lineStrings);
}
 
Example 18
/**
 * Convert the logs to a featurecollection.
 * 
 * @param pm the monitor.
 * @param logsList the list of logs as gathered from {@link #getGpsLogsList(IHMConnection)}.
 * @return the extracted collection.
 * @throws Exception
 */
public static DefaultFeatureCollection getLogLinesFeatureCollection( IHMProgressMonitor pm, List<GpsLog> logsList ) {
    GeometryFactory gf = GeometryUtilities.gf();
    SimpleFeatureType featureType = GeopaparazziUtilities.getGpsLogLinesFeatureType();
    pm.beginTask("Import gps to lines...", logsList.size());
    DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
    for( GpsLog log : logsList ) {
        List<GpsPoint> points = log.points;

        List<Coordinate> coordList = new ArrayList<>();
        String startDate = ETimeUtilities.INSTANCE.TIME_FORMATTER_LOCAL.format(new Date(log.startTime));
        String endDate = ETimeUtilities.INSTANCE.TIME_FORMATTER_LOCAL.format(new Date(log.endTime));
        for( GpsPoint gpsPoint : points ) {
            Coordinate c = new Coordinate(gpsPoint.lon, gpsPoint.lat);
            coordList.add(c);
        }
        Coordinate[] coordArray = coordList.toArray(new Coordinate[coordList.size()]);
        if (coordArray.length < 2) {
            continue;
        }
        LineString lineString = gf.createLineString(coordArray);
        MultiLineString multiLineString = gf.createMultiLineString(new LineString[]{lineString});

        SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
        Object[] values = new Object[]{multiLineString, startDate, endDate, log.text};
        builder.addAll(values);
        SimpleFeature feature = builder.buildFeature(null);

        newCollection.add(feature);
        pm.worked(1);
    }
    pm.done();
    return newCollection;
}
 
Example 19
public static SimpleFeatureType getGpsLogLinesFeatureType() {
    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
    b.setName("geopaparazzilogs");
    b.setCRS(DefaultGeographicCRS.WGS84);
    b.add("the_geom", MultiLineString.class);
    b.add(GPSLOG_startdateFN, String.class);
    b.add(GPSLOG_enddateFN, String.class);
    b.add(GPSLOG_descrFN, String.class);
    SimpleFeatureType featureType = b.buildFeatureType();
    return featureType;
}
 
Example 20
private MultiLineString readMultiLineString() throws IOException, ParseException {
    int numGeom = dis.readInt();
    LineString[] geoms = new LineString[numGeom];
    for( int i = 0; i < numGeom; i++ ) {
        Geometry g = readGeometry();
        if (!(g instanceof LineString))
            throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiLineString");
        geoms[i] = (LineString) g;
    }
    return factory.createMultiLineString(geoms);
}
 
Example 21
private Polygon smoothVectorBasin( Polygon polygon ) throws Exception {
    if (!doSmoothing) {
        return polygon;
    }

    // final PolygonSmoother polygonSmoother = new PolygonSmoother();
    pm.beginTask("Smoothing polygons...", IHMProgressMonitor.UNKNOWN);

    try {
        LineString lineString = gf.createLineString(polygon.getCoordinates());

        DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
        newCollection.add(FeatureUtilities.toDummyFeature(lineString, null));

        OmsLineSmootherMcMaster smoother = new OmsLineSmootherMcMaster();
        smoother.inVector = newCollection;
        smoother.pLookahead = 5;
        smoother.pSlide = 0.9;
        // smoother.pDensify = 0.9;
        smoother.process();
        SimpleFeatureCollection outFeatures = smoother.outVector;

        MultiLineString newGeom = (MultiLineString) outFeatures.features().next().getDefaultGeometry();
        polygon = gf.createPolygon(gf.createLinearRing(newGeom.getCoordinates()), null);
    } catch (Exception e) {
        pm.errorMessage("Warning, unable to smooth the basin. Continue with original layer.");
    }

    pm.done();

    return polygon;
}
 
Example 22
@Override
public ShapeFigure createLineFigure(Shape shape, FigureStyle style) {
    MultiLineString multiLineString = toJtsGeom.createMultiLineString(shape);
    Geometry geometry = multiLineString;
    if (multiLineString.getNumGeometries() == 1) {
        geometry = multiLineString.getGeometryN(0);
    }
    final Geometry geometryInSceneCoords;
    try {
        geometryInSceneCoords = sceneTransformProvider.getModelToSceneTransform().transform(geometry);
    } catch (TransformException e) {
        return null;
    }
    return createShapeFigure(geometryInSceneCoords, style);
}
 
Example 23
Source Project: java-vector-tile   Source File: VectorTileEncoder.java    License: Apache License 2.0 5 votes vote down vote up
List<Integer> commands(Geometry geometry) {
    
    if (geometry instanceof MultiLineString) {
        return commands((MultiLineString) geometry);
    }
    if (geometry instanceof Polygon) {
        return commands((Polygon) geometry);
    }
    if (geometry instanceof MultiPolygon) {
        return commands((MultiPolygon) geometry);
    }        
    
    return commands(geometry.getCoordinates(), shouldClosePath(geometry), geometry instanceof MultiPoint);
}
 
Example 24
Source Project: geowave   Source File: TWKBWriter.java    License: Apache License 2.0 5 votes vote down vote up
private void writeMultiLineString(
    final MultiLineString multiLine,
    final PrecisionWriter precision,
    final DataOutput output) throws IOException {
  Varint.writeUnsignedVarInt(multiLine.getNumGeometries(), output);
  for (int i = 0; i < multiLine.getNumGeometries(); i++) {
    precision.writePointArray(multiLine.getGeometryN(i).getCoordinates(), output);
  }
}
 
Example 25
Source Project: geowave   Source File: TWKBReader.java    License: Apache License 2.0 5 votes vote down vote up
private MultiLineString readMultiLineString(
    final PrecisionReader precision,
    final byte metadata,
    final ByteBuffer input) throws IOException {
  if ((metadata & TWKBUtils.EMPTY_GEOMETRY) != 0) {
    return GeometryUtils.GEOMETRY_FACTORY.createMultiLineString();
  }
  final int numLines = VarintUtils.readUnsignedInt(input);
  final LineString[] lines = new LineString[numLines];
  for (int i = 0; i < numLines; i++) {
    lines[i] = GeometryUtils.GEOMETRY_FACTORY.createLineString(precision.readPointArray(input));
  }
  return GeometryUtils.GEOMETRY_FACTORY.createMultiLineString(lines);
}
 
Example 26
Source Project: sis   Source File: GeometryTransform.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Transforms the given geometry. This method delegates to one of the {@code transform(…)} methods
 * based on the type of the given geometry.
 *
 * @param  geom  the geometry to transform.
 * @return the transformed geometry.
 * @throws TransformException if an error occurred while transforming the geometry.
 */
public Geometry transform(final Geometry geom) throws TransformException {
    if (geom instanceof Point)              return transform((Point)              geom);
    if (geom instanceof MultiPoint)         return transform((MultiPoint)         geom);
    if (geom instanceof LinearRing)         return transform((LinearRing)         geom);    // Must be tested before LineString.
    if (geom instanceof LineString)         return transform((LineString)         geom);
    if (geom instanceof MultiLineString)    return transform((MultiLineString)    geom);
    if (geom instanceof Polygon)            return transform((Polygon)            geom);
    if (geom instanceof MultiPolygon)       return transform((MultiPolygon)       geom);
    if (geom instanceof GeometryCollection) return transform((GeometryCollection) geom);
    throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedType_1, Classes.getClass(geom)));
}
 
Example 27
Source Project: sis   Source File: JTSTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link JTS#createPolyline(int, Vector...)}.
 */
@Test
@Override
public void testCreatePolyline() {
    super.testCreatePolyline();
    final MultiLineString mp = (MultiLineString) geometry;
    assertEquals("numGeometries", 2, mp.getNumGeometries());
    verifyTwoFirstGeometries(mp);
}
 
Example 28
Source Project: sis   Source File: JTSTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the coordinates of the two first geometries of the given multi line string.
 * If there is more than 2 geometries, it is caller responsibility to verify the other ones.
 */
private static void verifyTwoFirstGeometries(final MultiLineString mp) {
    assertArrayEquals(new Coordinate[] {
            new Coordinate(4, 5),
            new Coordinate(7, 9),
            new Coordinate(9, 3),
            new Coordinate(4, 5)}, mp.getGeometryN(0).getCoordinates());

    assertArrayEquals(new Coordinate[] {
            new Coordinate(-3, -2),
            new Coordinate(-2, -5),
            new Coordinate(-1, -6)}, mp.getGeometryN(1).getCoordinates());
}
 
Example 29
Source Project: sis   Source File: JTSTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link Geometries#tryMergePolylines(Object, Iterator)}.
 */
@Test
@Override
public void testTryMergePolylines() {
    super.testTryMergePolylines();
    final MultiLineString mp = (MultiLineString) geometry;
    assertEquals("numGeometries", 3, mp.getNumGeometries());
    verifyTwoFirstGeometries(mp);
    assertArrayEquals(new Coordinate[] {
            new Coordinate(13, 11),
            new Coordinate(14, 12),
            new Coordinate(15, 11),
            new Coordinate(13, 10)}, mp.getGeometryN(2).getCoordinates());
}
 
Example 30
Source Project: crate   Source File: GeoJSONUtils.java    License: 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);
}