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

The following examples show how to use org.locationtech.jts.geom.GeometryFactory#createPolygon() . 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: LasIndexer.java    From hortonmachine with GNU General Public License v3.0 7 votes vote down vote up
public static Polygon envelopeToPolygon( Envelope envelope ) {
    double w = envelope.getMinX();
    double e = envelope.getMaxX();
    double s = envelope.getMinY();
    double n = envelope.getMaxY();

    Coordinate[] coords = new Coordinate[5];
    coords[0] = new Coordinate(w, n);
    coords[1] = new Coordinate(e, n);
    coords[2] = new Coordinate(e, s);
    coords[3] = new Coordinate(w, s);
    coords[4] = new Coordinate(w, n);

    GeometryFactory gf = GeometryUtilities.gf();
    LinearRing linearRing = gf.createLinearRing(coords);
    Polygon polygon = gf.createPolygon(linearRing, null);
    return polygon;
}
 
Example 2
Source File: BinningFormModel.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
public Geometry getRegion() {
    if (Boolean.TRUE.equals(getPropertyValue(PROPERTY_KEY_GLOBAL))) {
        return toGeometry(GLOBAL_WKT);
    } else if (Boolean.TRUE.equals(getPropertyValue(PROPERTY_KEY_COMPUTE_REGION))) {
        return null;
    } else if (Boolean.TRUE.equals(getPropertyValue(PROPERTY_KEY_BOUNDS))) {
        final double westValue = getPropertyValue(PROPERTY_KEY_WEST_BOUND);
        final double eastValue = getPropertyValue(PROPERTY_KEY_EAST_BOUND);
        final double northValue = getPropertyValue(PROPERTY_KEY_NORTH_BOUND);
        final double southValue = getPropertyValue(PROPERTY_KEY_SOUTH_BOUND);
        Coordinate[] coordinates = {
                new Coordinate(westValue, southValue), new Coordinate(westValue, northValue),
                new Coordinate(eastValue, northValue), new Coordinate(eastValue, southValue),
                new Coordinate(westValue, southValue)
        };

        final GeometryFactory geometryFactory = new GeometryFactory();
        return geometryFactory.createPolygon(geometryFactory.createLinearRing(coordinates), null);
    } else if (Boolean.TRUE.equals(getPropertyValue(PROPERTY_KEY_MANUAL_WKT))) {
        return toGeometry(getPropertyValue(PROPERTY_KEY_WKT));
    }
    throw new IllegalStateException("Should never come here");
}
 
Example 3
Source File: SubsetUI.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
private void getGeoRegion() {
    geoRegion = null;
    geoText.setText("");
    if (geoCoordRadio.isSelected()) {
        final GeoPos[] selectionBox = worldMapUI.getSelectionBox();
        if (selectionBox != null) {
            final Coordinate[] coords = new Coordinate[selectionBox.length + 1];
            for (int i = 0; i < selectionBox.length; ++i) {
                coords[i] = new Coordinate(selectionBox[i].getLon(), selectionBox[i].getLat());
            }
            coords[selectionBox.length] = new Coordinate(selectionBox[0].getLon(), selectionBox[0].getLat());

            final GeometryFactory geometryFactory = new GeometryFactory();
            final LinearRing linearRing = geometryFactory.createLinearRing(coords);

            geoRegion = geometryFactory.createPolygon(linearRing, null);
            geoText.setText(geoRegion.toText());
        }
    }
}
 
Example 4
Source File: SpatialTemporalQueryTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws ParseException {
  final GeometryFactory factory = new GeometryFactory();
  final ExplicitSpatialTemporalQuery query =
      new ExplicitSpatialTemporalQuery(
          df.parse("2005-05-17T19:32:56GMT-00:00"),
          df.parse("2005-05-17T22:32:56GMT-00:00"),
          factory.createPolygon(
              new Coordinate[] {
                  new Coordinate(24, 33),
                  new Coordinate(28, 33),
                  new Coordinate(28, 31),
                  new Coordinate(24, 31),
                  new Coordinate(24, 33)}));
  final ExplicitSpatialTemporalQuery queryCopy = new ExplicitSpatialTemporalQuery();
  queryCopy.fromBinary(query.toBinary());
  assertEquals(queryCopy.getQueryGeometry(), query.getQueryGeometry());
}
 
Example 5
Source File: FeatureUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a {@link Polygon} from an {@link Envelope}.
 * 
 * @param envelope the envelope to convert.
 * @return the created polygon.
 */
public static Polygon envelopeToPolygon( Envelope envelope ) {
    double w = envelope.getMinX();
    double e = envelope.getMaxX();
    double s = envelope.getMinY();
    double n = envelope.getMaxY();

    Coordinate[] coords = new Coordinate[5];
    coords[0] = new Coordinate(w, n);
    coords[1] = new Coordinate(e, n);
    coords[2] = new Coordinate(e, s);
    coords[3] = new Coordinate(w, s);
    coords[4] = new Coordinate(w, n);

    GeometryFactory gf = GeometryUtilities.gf();
    LinearRing linearRing = gf.createLinearRing(coords);
    Polygon polygon = gf.createPolygon(linearRing, null);
    return polygon;
}
 
Example 6
Source File: ExportGeometryActionTest.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testWritingShapeFile_Geometry() throws Exception {
    SimpleFeatureType sft = createPlainFeatureType("Polygon", Geometry.class, DefaultGeographicCRS.WGS84);

    GeometryFactory gf = new GeometryFactory();
    Polygon polygon = gf.createPolygon(gf.createLinearRing(new Coordinate[]{
            new Coordinate(0, 0),
            new Coordinate(1, 0),
            new Coordinate(0, 1),
            new Coordinate(0, 0),
    }), null);
    SimpleFeature polygonFeature = createPlainFeature(sft, "_1", polygon, "");


    ArrayList<SimpleFeature> features = new ArrayList<>();
    features.add(polygonFeature);
    Class<Polygon> geomType = Polygon.class;
    doExportImport(features, geomType);
}
 
Example 7
Source File: ElasticGeometryFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testContainsFilter() throws Exception {
    init("not-active","geo3");
    FilterFactory2 ff = (FilterFactory2) dataStore.getFilterFactory();
    GeometryFactory gf = new GeometryFactory();
    PackedCoordinateSequenceFactory sf = new PackedCoordinateSequenceFactory();
    Polygon ls = gf.createPolygon(sf.create(new double[] { 2, 2, 3, 2, 3, 3, 2, 3, 2, 2 }, 2));
    Contains f = ff.contains(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 8
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a bounds polygon of a {@link GridCoverage2D}.
 * 
 * @param gridCoverage the coverage to use.
 * @return the bounding polygon.
 */
public static Polygon getRegionPolygon( GridCoverage2D gridCoverage ) {
    Envelope2D env = gridCoverage.getEnvelope2D();
    Coordinate[] c = new Coordinate[]{new Coordinate(env.getMinX(), env.getMinY()),
            new Coordinate(env.getMinX(), env.getMaxY()), new Coordinate(env.getMaxX(), env.getMaxY()),
            new Coordinate(env.getMaxX(), env.getMinY()), new Coordinate(env.getMinX(), env.getMinY())};
    GeometryFactory gf = GeometryUtilities.gf();
    LinearRing linearRing = gf.createLinearRing(c);
    Polygon polygon = gf.createPolygon(linearRing, null);
    return polygon;
}
 
Example 9
Source File: FastLiteShape.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public FastLiteShape( Geometry geom ) {
    super(geom, new AffineTransform(), false);
    this.prepared = PreparedGeometryFactory.prepare(geom);
    GeometryFactory gf = new GeometryFactory();
    pointCS = new LiteCoordinateSequence(1, 2);
    point = gf.createPoint(pointCS);
    rectCS = new LiteCoordinateSequence(5, 2);
    rect = gf.createPolygon(gf.createLinearRing(rectCS), null);
    // System.out.println("Crop area: " + geom);
}
 
Example 10
Source File: Polygon.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * To geometry
 *
 * @param factory GeometryFactory
 * @return Geometry
 */
public Geometry toGeometry(GeometryFactory factory) {
    PointD p;
    Coordinate[] cs = new Coordinate[_outLine.size()];
    for (int i = 0; i < cs.length; i++) {
        p = _outLine.get(i);
        cs[i] = new Coordinate(p.X, p.Y);
    }
    if (cs[0].x != cs[cs.length -1].x){
        cs = (Coordinate[])DataConvert.resizeArray(cs, cs.length + 1);
        cs[cs.length - 1] = new Coordinate(cs[0].x, cs[1].y);
    }
    LinearRing shell = factory.createLinearRing(cs);
    LinearRing[] holes = new LinearRing[this._holeLines.size()];
    int n;
    boolean isclose;
    for (int j = 0; j < holes.length; j++) {
        List<? extends PointD> hole = this._holeLines.get(j);
        n = hole.size();
        isclose = true;
        if (n == 3) {
            n = 4;
            isclose = false;
        }
        cs = new Coordinate[n];
        for (int i = 0; i < hole.size(); i++) {
            p = hole.get(i);
            cs[i] = new Coordinate(p.X, p.Y);
        }      
        if (!isclose){
            cs[n - 1] = new Coordinate(hole.get(0).X, hole.get(0).Y);
        }
        holes[j] = factory.createLinearRing(cs);
    }
    return factory.createPolygon(shell, holes);
}
 
Example 11
Source File: ElasticGeometryFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testIntersectsFilter() throws Exception {
    init("not-active","geo3");
    FilterFactory2 ff = (FilterFactory2) dataStore.getFilterFactory();
    GeometryFactory gf = new GeometryFactory();
    PackedCoordinateSequenceFactory sf = new PackedCoordinateSequenceFactory();
    Polygon ls = gf.createPolygon(sf.create(new double[] { 6, 6, 7, 6, 7, 7, 6, 7, 6, 6 }, 2));
    Intersects f = ff.intersects(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.13");
}
 
Example 12
Source File: ElasticGeometryFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testWithinFilter() throws Exception {
    init("not-active","geo3");
    FilterFactory2 ff = (FilterFactory2) dataStore.getFilterFactory();
    GeometryFactory gf = new GeometryFactory();
    PackedCoordinateSequenceFactory sf = new PackedCoordinateSequenceFactory();
    Polygon ls = gf.createPolygon(sf.create(new double[] { 0, 0, 0, 6, 6, 6, 6, 0, 0, 0 }, 2));
    Within f = ff.within(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 13
Source File: FeatureLayerConfigurationPersistencyTest.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Layer createLayer(LayerType layerType) throws Exception {

    final PropertySet configuration = layerType.createLayerConfig(null);

    final URL shapefileUrl = getClass().getResource("bundeslaender.shp");
    configuration.setValue(FeatureLayerType.PROPERTY_NAME_FEATURE_COLLECTION_URL, shapefileUrl);
    configuration.setValue(FeatureLayerType.PROPERTY_NAME_FEATURE_COLLECTION_CRS, DefaultGeographicCRS.WGS84);
    final Coordinate[] coordinates = {
            new Coordinate(-10, 50),
            new Coordinate(+10, 50),
            new Coordinate(+10, 30),
            new Coordinate(-10, 30),
            new Coordinate(-10, 50)
    };
    final GeometryFactory geometryFactory = new GeometryFactory();
    final LinearRing ring = geometryFactory.createLinearRing(coordinates);
    final Polygon clipGeometry = geometryFactory.createPolygon(ring, new LinearRing[0]);
    configuration.setValue(FeatureLayerType.PROPERTY_NAME_FEATURE_COLLECTION_CLIP_GEOMETRY, clipGeometry);
    configuration.setValue(FeatureLayerType.PROPERTY_NAME_SLD_STYLE, createStyle());
    FeatureCollection<SimpleFeatureType, SimpleFeature> fc;
    try {
        fc = FeatureUtils.createFeatureCollection(
                shapefileUrl, DefaultGeographicCRS.WGS84, clipGeometry);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return new FeatureLayer(layerType, fc, configuration);
}
 
Example 14
Source File: ElasticGeometryFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testWithinPolygonFilter() throws Exception {
    init();
    FilterFactory2 ff = (FilterFactory2) dataStore.getFilterFactory();
    GeometryFactory gf = new GeometryFactory();
    PackedCoordinateSequenceFactory sf = new PackedCoordinateSequenceFactory();
    Polygon ls = gf.createPolygon(sf.create(new double[] { -180, -90, 180, -90, 180, 90, -180, 90, -180, -90 }, 2));
    Within f = ff.within(ff.property("geo"), ff.literal(ls));
    SimpleFeatureCollection features = featureSource.getFeatures(f);
    assertEquals(11, features.size());
}
 
Example 15
Source File: GeoJSONDecoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected Polygon decodePolygonCoordinates(JsonNode coordinates, GeometryFactory fac)
        throws GeoJSONDecodingException {
    if (!coordinates.isArray()) {
        throw new GeoJSONDecodingException(EXPECTED_ARRAY);
    }
    if (coordinates.size() < 1) {
        throw new GeoJSONDecodingException("missing polygon shell");
    }
    LinearRing shell = fac.createLinearRing(decodeCoordinates(coordinates.get(0)));
    LinearRing[] holes = new LinearRing[coordinates.size() - 1];
    for (int i = 1; i < coordinates.size(); ++i) {
        holes[i - 1] = fac.createLinearRing(decodeCoordinates(coordinates.get(i)));
    }
    return fac.createPolygon(shell, holes);
}
 
Example 16
Source File: SpatialQueryTest.java    From geowave with Apache License 2.0 4 votes vote down vote up
public void performOp(final CompareOperation op, final boolean[] expectedResults) {
  final GeometryFactory factory = new GeometryFactory();
  // query geometry for testing
  final Coordinate[] queryCoord =
      new Coordinate[] {
          new Coordinate(24, 33),
          new Coordinate(28, 33),
          new Coordinate(28, 37),
          new Coordinate(24, 37),
          new Coordinate(24, 33)};
  // create spatial query object with geometric relationship operator
  final ExplicitSpatialQuery query =
      new ExplicitSpatialQuery(factory.createPolygon(queryCoord), op);

  final ExplicitSpatialQuery queryCopy = new ExplicitSpatialQuery();
  queryCopy.fromBinary(query.toBinary());

  // This line is crossing query polygon
  final Coordinate[] line1 = new Coordinate[] {new Coordinate(22, 32), new Coordinate(25, 36)};
  // This line is completely within the query polygon
  final Coordinate[] line2 = new Coordinate[] {new Coordinate(25, 33.5), new Coordinate(26, 34)};
  // This line is completely outside of the query polygon
  final Coordinate[] line3 = new Coordinate[] {new Coordinate(21, 33.5), new Coordinate(23, 34)};
  // This line is touching one of the corner of the query polygon
  final Coordinate[] line4 = new Coordinate[] {new Coordinate(28, 33), new Coordinate(30, 34)};
  // this polygon is completely contained within the query polygon
  final Coordinate[] smallPolygon =
      new Coordinate[] {
          new Coordinate(25, 34),
          new Coordinate(27, 34),
          new Coordinate(27, 36),
          new Coordinate(25, 36),
          new Coordinate(25, 34)};

  // this polygon is same as query polygon
  final Coordinate[] dataPolygon = queryCoord.clone();

  final IndexedPersistenceEncoding[] data =
      new IndexedPersistenceEncoding[] {
          createData(factory.createLineString(line1)),
          createData(factory.createLineString(line2)),
          createData(factory.createLineString(line3)),
          createData(factory.createLineString(line4)),
          createData(factory.createPolygon(smallPolygon)),
          createData(factory.createPolygon(dataPolygon))};

  int pos = 0;
  final Index index = new SpatialDimensionalityTypeProvider().createIndex(new SpatialOptions());
  for (final IndexedPersistenceEncoding dataItem : data) {
    for (final QueryFilter filter : queryCopy.createFilters(index)) {
      assertEquals(
          "result: " + pos,
          expectedResults[pos++],
          filter.accept(index.getIndexModel(), dataItem));
    }
  }
}
 
Example 17
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Extracts a list of polygons from the cell bounds of a given {@link GridCoverage2D coverage}.
 * 
 * <p><b>Note that the cells are added in a rows 
 * and cols order (for each row evaluate each column).</b></p> 
 * 
 * <p>The userdata of the geometry contains the value of the raster.
 * 
 * @param coverage the coverage to use.
 * @param keepCoordinatePredicate an optional predicate to filter out some of the cells.
 * @return the list of envelope geometries.
 */
public static List<Polygon> gridcoverageToCellPolygons( GridCoverage2D coverage,
        Predicate<Coordinate> keepCoordinatePredicate ) {
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(coverage);
    double west = regionMap.getWest();
    double north = regionMap.getNorth();
    double xres = regionMap.getXres();
    double yres = regionMap.getYres();
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();

    GeometryFactory gf = GeometryUtilities.gf();
    RandomIter iter = CoverageUtilities.getRandomIterator(coverage);
    List<Polygon> polygons = new ArrayList<Polygon>();
    for( int r = 0; r < rows; r++ ) {
        for( int c = 0; c < cols; c++ ) {
            double w = west + xres * c;
            double e = w + xres;
            double n = north - yres * r;
            double s = n - yres;

            if (keepCoordinatePredicate != null
                    && !keepCoordinatePredicate.test(new Coordinate(w + xres / 2, s + yres / 2))) {
                continue;
            }

            Coordinate[] coords = new Coordinate[5];
            coords[0] = new Coordinate(w, n);
            coords[1] = new Coordinate(e, n);
            coords[2] = new Coordinate(e, s);
            coords[3] = new Coordinate(w, s);
            coords[4] = new Coordinate(w, n);

            LinearRing linearRing = gf.createLinearRing(coords);
            Polygon polygon = gf.createPolygon(linearRing, null);
            polygons.add(polygon);

            double value = iter.getSampleDouble(c, r, 0);
            polygon.setUserData(value);
        }
    }
    return polygons;
}
 
Example 18
Source File: ExamplePolygonImplIOM.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Polygon getPolygon() {
    if (polygon == null) {
        // CHECKSTYLE:OFF
        double[][] rawLocations =
                new double[][] {
                    {-4.652710, 54.069059},
                    {-4.634857, 54.075506},
                    {-4.629364, 54.059388},
                    {-4.600525, 54.087590},
                    {-4.574432, 54.102892},
                    {-4.548340, 54.103697},
                    {-4.522247, 54.124626},
                    {-4.476929, 54.143132},
                    {-4.470062, 54.162434},
                    {-4.428864, 54.169670},
                    {-4.383545, 54.194583},
                    {-4.398651, 54.209846},
                    {-4.397278, 54.223496},
                    {-4.373932, 54.229919},
                    {-4.364319, 54.249180},
                    {-4.301147, 54.303704},
                    {-4.372559, 54.315722},
                    {-4.380798, 54.344550},
                    {-4.365692, 54.389354},
                    {-4.364319, 54.420528},
                    {-4.459076, 54.402946},
                    {-4.534607, 54.373359},
                    {-4.578552, 54.322931},
                    {-4.601898, 54.285270},
                    {-4.636230, 54.258807},
                    {-4.671936, 54.237143},
                    {-4.703522, 54.229919},
                    {-4.728241, 54.187352},
                    {-4.743347, 54.173689},
                    {-4.735107, 54.143132},
                    {-4.755707, 54.110138},
                    {-4.783173, 54.101281},
                    {-4.777679, 54.086784},
                    {-4.822998, 54.049714},
                    {-4.737854, 54.066642},
                    {-4.709015, 54.082757},
                    {-4.682922, 54.062612},
                    {-4.652710, 54.069059},
                };
        // 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++;
        }

        LinearRing ring = geometryFactory.createLinearRing(coords);
        LinearRing holes[] = null; // use LinearRing[] to represent holes
        polygon = geometryFactory.createPolygon(ring, holes);
    }
    return polygon;
}
 
Example 19
Source File: JTSHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
public static Geometry createPolygonFromEnvelope(double minx, double miny, double maxx, double maxy, int srid) {
    GeometryFactory fac = getGeometryFactoryForSRID(srid);
    return fac.createPolygon(new Coordinate[] { new Coordinate(minx, miny), new Coordinate(minx, maxy),
        new Coordinate(maxx, maxy), new Coordinate(maxx, miny), new Coordinate(minx, miny) });
}
 
Example 20
Source File: GeometryUtils.java    From geowave with Apache License 2.0 2 votes vote down vote up
/**
 * Return a multi-polygon representing the bounded map regions split by the axis
 *
 * @param factory
 * @param crs
 * @return a world geometry
 */
public static Geometry world(final GeometryFactory factory, final CoordinateReferenceSystem crs) {
  return factory.createPolygon(toPolygonCoordinates(crs.getCoordinateSystem()));
}