Java Code Examples for org.locationtech.jts.geom.GeometryFactory#createLineString()

The following examples show how to use org.locationtech.jts.geom.GeometryFactory#createLineString() . 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: GeoWaveGeometryPrecisionIT.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testNegativePrecision() {
  final GeometryFactory factory = GeometryUtils.GEOMETRY_FACTORY;
  final Geometry[] geometries =
      new Geometry[] {
          factory.createPoint(new Coordinate(12.123456789, -10.987654321)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456789.987654321, -123456789.987654321),
                  new Coordinate(987654321.123456789, -987654321.123456789)}),
          factory.createPoint(new Coordinate(0, 0))};
  final Geometry[] expected =
      new Geometry[] {
          factory.createPoint(new Coordinate(0, 0)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123457000, -123457000),
                  new Coordinate(987654000, -987654000)}),
          factory.createPoint(new Coordinate(0, 0))};
  testPrecision(geometries, expected, -3);
}
 
Example 2
Source File: FilterToElastic.java    From elasticgeo with GNU General Public License v3.0 6 votes vote down vote up
private void visitLiteralGeometry(Literal expression) throws IOException {
    // evaluate the literal and store it for later
    currentGeometry  = (Geometry) evaluateLiteral(expression, Geometry.class);

    if ( currentGeometry instanceof LinearRing ) {
        // convert LinearRing to LineString
        final GeometryFactory factory = currentGeometry.getFactory();
        final LinearRing linearRing = (LinearRing) currentGeometry;
        final CoordinateSequence coordinates;
        coordinates = linearRing.getCoordinateSequence();
        currentGeometry = factory.createLineString(coordinates);
    }

    final String geoJson = new GeometryJSON().toString(currentGeometry);
    currentShapeBuilder = mapReader.readValue(geoJson);
}
 
Example 3
Source File: GeoWaveGeometryPrecisionIT.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrecision0() {
  final GeometryFactory factory = GeometryUtils.GEOMETRY_FACTORY;
  final Geometry[] geometries =
      new Geometry[] {
          factory.createPoint(new Coordinate(12.123456789, -10.987654321)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456789.987654321, -123456789.987654321),
                  new Coordinate(987654321.123456789, -987654321.123456789)}),
          factory.createPoint(new Coordinate(0, 0))};
  final Geometry[] expected =
      new Geometry[] {
          factory.createPoint(new Coordinate(12, -11)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456790, -123456790),
                  new Coordinate(987654321, -987654321)}),
          factory.createPoint(new Coordinate(0, 0))};
  testPrecision(geometries, expected, 0);
}
 
Example 4
Source File: GeoWaveGeometryPrecisionIT.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrecision3() {
  final GeometryFactory factory = GeometryUtils.GEOMETRY_FACTORY;
  final Geometry[] geometries =
      new Geometry[] {
          factory.createPoint(new Coordinate(12.123456789, -10.987654321)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456789.987654321, -123456789.987654321),
                  new Coordinate(987654321.123456789, -987654321.123456789)}),
          factory.createPoint(new Coordinate(0, 0))};
  final Geometry[] expected =
      new Geometry[] {
          factory.createPoint(new Coordinate(12.123, -10.988)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456789.988, -123456789.988),
                  new Coordinate(987654321.123, -987654321.123)}),
          factory.createPoint(new Coordinate(0, 0))};
  testPrecision(geometries, expected, 3);
}
 
Example 5
Source File: ElasticGeometryFilterIT.java    From elasticgeo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGeoShapeAsWkt() throws Exception {
    if (client.getVersion() < 6.2) {
        // wkt unsupported prior to v6.2
        return;
    }
    init("not-active","geo6");
    FilterFactory2 ff = (FilterFactory2) dataStore.getFilterFactory();
    GeometryFactory gf = new GeometryFactory();
    PackedCoordinateSequenceFactory sf = new PackedCoordinateSequenceFactory();
    LineString ls = gf.createLineString(sf.create(new double[] { 0, 0, 2, 2 }, 2));
    Crosses f = ff.crosses(ff.property("geo3"), ff.literal(ls));
    SimpleFeatureCollection features = featureSource.getFeatures(f);
    assertEquals(1, features.size());
    SimpleFeatureIterator fsi = features.features();
    assertTrue(fsi.hasNext());
    assertEquals(fsi.next().getID(), "active.12");

    sf = new PackedCoordinateSequenceFactory();
    ls = gf.createLineString(sf.create(new double[] { 0, 0, 1, 1 }, 2));
    f = ff.crosses(ff.property("geo5"), ff.literal(ls));
    features = featureSource.getFeatures(f);
    assertEquals(0, features.size());
}
 
Example 6
Source File: GeoWaveGeometryPrecisionIT.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testMaxPrecision() {
  final GeometryFactory factory = GeometryUtils.GEOMETRY_FACTORY;
  final Geometry[] geometries =
      new Geometry[] {
          factory.createPoint(new Coordinate(12.123456789, -10.987654321)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456789.987654321, -123456789.987654321),
                  new Coordinate(987654321.123456789, -987654321.123456789)}),
          factory.createPoint(new Coordinate(0, 0))};
  final Geometry[] expected =
      new Geometry[] {
          factory.createPoint(new Coordinate(12.1234568, -10.9876543)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456789.9876543, -123456789.9876543),
                  new Coordinate(987654321.1234568, -987654321.1234568)}),
          factory.createPoint(new Coordinate(0, 0))};
  testPrecision(geometries, expected, GeometryUtils.MAX_GEOMETRY_PRECISION);
}
 
Example 7
Source File: CommonQueries.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Calculates the GeodesicLength between to points.
 * 
 * @param connection the database connection.
 * @param p1 the first point.
 * @param p2 the second point.
 * @param srid the srid. If <0, 4326 will be used. This needs to be a geographic prj.
 * @return the distance.
 * @throws Exception
 */
public static double getDistanceBetween( IHMConnection connection, Coordinate p1, Coordinate p2, int srid ) throws Exception {
    if (srid < 0) {
        srid = 4326;
    }
    GeometryFactory gf = new GeometryFactory();
    LineString lineString = gf.createLineString(new Coordinate[]{p1, p2});
    String sql = "select GeodesicLength(LineFromText(\"" + lineString.toText() + "\"," + srid + "));";
    try (IHMStatement stmt = connection.createStatement(); IHMResultSet rs = stmt.executeQuery(sql);) {
        if (rs.next()) {
            double length = rs.getDouble(1);
            return length;
        }
        throw new RuntimeException("Could not calculate distance.");
    }

}
 
Example 8
Source File: OmsGeopaparazzi4Converter.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 9
Source File: GpsLogsLayer.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
public void reloadData() throws IOException {
    SQLiteDatabase sqliteDatabase = GPApplication.getInstance().getDatabase();
    List<GpsLog> logsList = MapUtilities.getGpsLogs(sqliteDatabase);
    GeometryFactory gf = new GeometryFactory();

    tmpDrawables.clear();
    mDrawables.clear();
    for (GpsLog gpsLog : logsList) {
        LineString lineString = gf.createLineString(gpsLog.gpslogGeoPoints.toArray(new Coordinate[0]));
        Style lineStyle = Style.builder()
                .strokeColor(ColorUtilities.toColor(gpsLog.color))
                .strokeWidth((float) gpsLog.width)
                .cap(Paint.Cap.ROUND)
                .build();
        add(new LineDrawable(lineString, lineStyle));

        Point startPoint = lineString.getStartPoint();

        Style pointStyle = Style.builder()
                .buffer(gpsLog.width)
                .fillColor(ColorUtilities.toColor(gpsLog.color))
                .strokeColor(ColorUtilities.toColor(gpsLog.color))
                .scaleZoomLevel(19)
                .fillAlpha(1)
                .build();
        add(new PointDrawable(startPoint.getY(), startPoint.getX(), pointStyle));
    }
    update();
}
 
Example 10
Source File: GeoWaveGeometryPrecisionIT.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void testFullPrecision() {
  final GeometryFactory factory = GeometryUtils.GEOMETRY_FACTORY;
  final Geometry[] geometries =
      new Geometry[] {
          factory.createPoint(new Coordinate(12.123456789, -10.987654321)),
          factory.createLineString(
              new Coordinate[] {
                  new Coordinate(123456789.987654321, -123456789.987654321),
                  new Coordinate(987654321.123456789, -987654321.123456789)}),
          factory.createPoint(new Coordinate(0, 0))};
  testPrecision(geometries, geometries, null);
}
 
Example 11
Source File: TestGeometryUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public void testLines2Polygon() throws Exception {
    GeometryFactory gf = GeometryUtilities.gf();

    LineString l1 = gf.createLineString(new Coordinate[]{negll, negul, ul});
    LineString l2 = gf.createLineString(new Coordinate[]{ur, lr});
    LineString l3 = gf.createLineString(new Coordinate[]{ll, lr});

    Polygon lines2Polygon = GeometryUtilities.lines2Polygon(true, l1, l2, l3);

    Coordinate[] polygonCoord = new Coordinate[]{negll, negul, ul, ur, lr, lr, ll, negll};

    LinearRing linearRing = gf.createLinearRing(polygonCoord);
    Polygon expectedPolygon = gf.createPolygon(linearRing, null);
    assertTrue(lines2Polygon.equalsExact(expectedPolygon));
}
 
Example 12
Source File: GeoJSONDecoder.java    From arctic-sea with 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 13
Source File: ElasticGeometryFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testNotCrossesFilter() throws Exception {
    init("not-active","geo3");
    FilterFactory2 ff = (FilterFactory2) dataStore.getFilterFactory();
    GeometryFactory gf = new GeometryFactory();
    PackedCoordinateSequenceFactory sf = new PackedCoordinateSequenceFactory();
    LineString ls = gf.createLineString(sf.create(new double[] { 0, 0, 1, 1 }, 2));
    Crosses f = ff.crosses(ff.property("geo3"), ff.literal(ls));
    SimpleFeatureCollection features = featureSource.getFeatures(f);
    assertEquals(0, features.size());
}
 
Example 14
Source File: ElasticGeometryFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCrossesFilter() throws Exception {
    init("not-active","geo3");
    FilterFactory2 ff = (FilterFactory2) dataStore.getFilterFactory();
    GeometryFactory gf = new GeometryFactory();
    PackedCoordinateSequenceFactory sf = new PackedCoordinateSequenceFactory();
    LineString ls = gf.createLineString(sf.create(new double[] { 0, 0, 2, 2 }, 2));
    Crosses f = ff.crosses(ff.property("geo3"), ff.literal(ls));
    SimpleFeatureCollection features = featureSource.getFeatures(f);
    assertEquals(1, features.size());
    SimpleFeatureIterator fsi = features.features();
    assertTrue(fsi.hasNext());
    assertEquals(fsi.next().getID(), "active.12");
}
 
Example 15
Source File: GrassLegacyUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Fill polygon areas mapping on a raster
 * 
 * @param active the active region
 * @param polygon the jts polygon geometry
 * @param raster the empty raster data to be filled
 * @param rasterToMap the map from which the values to fill raster are taken (if null, the value
 *        parameter is used)
 * @param value the value to set if a second map is not given
 * @param monitor
 */
public static void rasterizePolygonGeometry( Window active, Geometry polygon, RasterData raster, RasterData rasterToMap,
        double value, IHMProgressMonitor monitor ) {
    GeometryFactory gFactory = new GeometryFactory();
    int rows = active.getRows();
    int cols = active.getCols();
    double delta = active.getWEResolution() / 4.0;

    monitor.beginTask("rasterizing...", rows); //$NON-NLS-1$
    for( int i = 0; i < rows; i++ ) {
        monitor.worked(1);
        // do scan line to fill the polygon
        Coordinate west = rowColToCenterCoordinates(active, i, 0);
        Coordinate east = rowColToCenterCoordinates(active, i, cols - 1);
        LineString line = gFactory.createLineString(new Coordinate[]{west, east});
        if (polygon.intersects(line)) {
            Geometry internalLines = polygon.intersection(line);
            Coordinate[] coords = internalLines.getCoordinates();
            for( int j = 0; j < coords.length; j = j + 2 ) {
                Coordinate startC = new Coordinate(coords[j].x + delta, coords[j].y);
                Coordinate endC = new Coordinate(coords[j + 1].x - delta, coords[j + 1].y);
                int[] startcol = coordinateToNearestRowCol(active, startC);
                int[] endcol = coordinateToNearestRowCol(active, endC);

                if (startcol == null || endcol == null) {
                    // vertex is outside of the region, ignore it
                    continue;
                }
                /*
                 * the part in between has to be filled
                 */
                for( int k = startcol[1]; k <= endcol[1]; k++ ) {
                    if (rasterToMap != null) {
                        raster.setValueAt(i, k, rasterToMap.getValueAt(i, k));
                    } else {
                        raster.setValueAt(i, k, value);
                    }
                }
            }

        }
    }
}
 
Example 16
Source File: ExampleLineImpl.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public LineString getLine() {
    if (line == null) {
        // CHECKSTYLE:OFF
        double[][] rawLocations =
                new double[][] {
                    {-123.167725, 48.502048},
                    {-123.464355, 48.297812},
                    {-124.738770, 48.603858},
                    {-125.189209, 48.828566},
                    {-125.112305, 48.951366},
                    {-125.507812, 48.929718},
                    {-125.870361, 49.145784},
                    {-126.035156, 49.167339},
                    {-126.112061, 49.253465},
                    {-126.243896, 49.282140},
                    {-126.287842, 49.360912},
                    {-126.397705, 49.410973},
                    {-126.573486, 49.375220},
                    {-126.584473, 49.560852},
                    {-126.815186, 49.610710},
                    {-127.012939, 49.745781},
                    {-126.947021, 49.788357},
                    {-127.166748, 49.852152},
                    {-127.518311, 50.113533},
                    {-127.814941, 50.078295},
                    {-127.957764, 50.120578},
                    {-127.825928, 50.254230},
                    {-128.012695, 50.331436},
                    {-127.946777, 50.450509},
                    {-128.122559, 50.457504},
                    {-128.364258, 50.652943},
                    {-128.342285, 50.792047},
                    {-128.100586, 50.882243},
                    {-127.858887, 50.944584},
                    {-127.518311, 50.798991},
                    {-127.221680, 50.639010}
                };
        // CHECKSTYLE:ON
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

        Coordinate[] coords = new Coordinate[rawLocations.length];
        int index = 0;
        for (double[] point : rawLocations) {
            Coordinate c = new Coordinate(point[0], point[1]);

            coords[index] = c;

            index++;
        }

        line = geometryFactory.createLineString(coords);
    }
    return line;
}
 
Example 17
Source File: GeoJSONDecoder.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
protected LineString decodeLineString(JsonNode node, GeometryFactory fac)
        throws GeoJSONDecodingException {
    Coordinate[] coordinates = decodeCoordinates(requireCoordinates(node));
    return fac.createLineString(coordinates);
}