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

The following examples show how to use org.locationtech.jts.geom.GeometryFactory#createLinearRing() . 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: 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( Envelope2D 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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
Source File: ExamplePolygonImpl.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.49210295036, 54.4153472858},
                    {-4.4634856663, 54.4269825687},
                    {-4.43426958965, 54.4117153967},
                    {-4.40869623532, 54.4326291409},
                    {-4.32782927985, 54.4641980089},
                    {-4.3659606463, 54.4197683392},
                    {-4.33467823679, 54.4265693547},
                    {-4.32454274819, 54.4024986924},
                    {-4.34126081686, 54.3660155026},
                    {-4.3424304253, 54.3042112639},
                    {-4.37506398925, 54.3014094498},
                    {-4.41392105869, 54.2658635384},
                    {-4.44375514123, 54.2532227674},
                    {-4.44763651915, 54.196776024},
                    {-4.48315404347, 54.1850220956},
                    {-4.52311962815, 54.1455956993},
                    {-4.58362722513, 54.1091637546},
                    {-4.62431015799, 54.0527236394},
                    {-4.71452726534, 54.0188283696},
                    {-4.71863162723, 54.0497614848},
                    {-4.75157122164, 54.0647816773},
                    {-4.79755603397, 54.0685543663},
                    {-4.79717105693, 54.122792557},
                    {-4.74451711581, 54.1875314993},
                    {-4.73842361793, 54.2081776896},
                    {-4.71656215204, 54.2185876346},
                    {-4.71759940991, 54.2322672444},
                    {-4.73514361565, 54.2446507516},
                    {-4.69488449392, 54.2771110727},
                    {-4.65558887927, 54.2914459801},
                    {-4.65220617099, 54.3116519242},
                    {-4.63949760848, 54.3400051903},
                    {-4.58879948143, 54.3629767901},
                    {-4.57315512904, 54.3829958979},
                    {-4.54023908795, 54.387968746},
                    {-4.51678123729, 54.4207829193},
                    {-4.50855200379, 54.405875113},
                    {-4.49210295036, 54.4153472858},
                };
        // 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 12
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 13
Source File: PolygonBuilder.java    From crate with Apache License 2.0 4 votes vote down vote up
protected static LinearRing linearRing(GeometryFactory factory, List<Coordinate> coordinates) {
    return factory.createLinearRing(coordinates.toArray(new Coordinate[coordinates.size()]));
}