Java Code Examples for org.locationtech.jts.geom.Geometry#getArea()
The following examples show how to use
org.locationtech.jts.geom.Geometry#getArea() .
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: DBScanClusterList.java From geowave with Apache License 2.0 | 6 votes |
protected double interpolateFactor(final Geometry areaBeingMerged) { try { if (clusterGeo == null) { return 1.0; } final Geometry intersection = areaBeingMerged.intersection(clusterGeo); final double geo2Area = areaBeingMerged.getArea(); if (intersection != null) { if ((intersection instanceof Point) && (areaBeingMerged instanceof Point)) { return 0.0; } else if (intersection.isEmpty()) { return 1.0; } else if (geo2Area > 0) { return 1.0 - (intersection.getArea() / geo2Area); } else { return 0.0; } } return 1.0; } catch (final Exception ex) { LOGGER.warn("Cannot calculate difference of geometries to interpolate size ", ex); } return 0.0; }
Example 2
Source File: ExtractGeometryFilterVisitor.java From geowave with Apache License 2.0 | 6 votes |
/** * @param filter * @param crs * @return null if empty constraint (infinite not supported) */ public static ExtractGeometryFilterVisitorResult getConstraints( final Filter filter, final CoordinateReferenceSystem crs, final String attributeOfInterest) { final ExtractGeometryFilterVisitorResult geoAndCompareOpData = (ExtractGeometryFilterVisitorResult) filter.accept( new ExtractGeometryFilterVisitor(crs, attributeOfInterest), null); if (geoAndCompareOpData == null) { return null; } final Geometry geo = geoAndCompareOpData.getGeometry(); // empty or infinite geometry simply return null as we can't create // linear constraints from if ((geo == null) || geo.isEmpty()) { return null; } final double area = geo.getArea(); if (Double.isInfinite(area) || Double.isNaN(area)) { return null; } return geoAndCompareOpData; }
Example 3
Source File: GeometryUtilities.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
/** * Scales a {@link Polygon} to have an unitary area. * * @param polygon the geometry to scale. * @return a copy of the scaled geometry. * @throws Exception */ public static Geometry scaleToUnitaryArea( Geometry polygon ) throws Exception { double area = polygon.getArea(); double scale = sqrt(1.0 / area); AffineTransform scaleAT = new AffineTransform(); scaleAT.scale(scale, scale); AffineTransform2D scaleTransform = new AffineTransform2D(scaleAT); polygon = JTS.transform(polygon, scaleTransform); return polygon; }
Example 4
Source File: TestVectorizer.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
public void testVectorizer1() throws Exception { double[][] inData = HMTestMaps.extractNet0Data; HashMap<String, Double> envelopeParams = HMTestMaps.getEnvelopeparams(); CoordinateReferenceSystem crs = HMTestMaps.getCrs(); GridCoverage2D inCoverage = CoverageUtilities.buildCoverage("data", inData, envelopeParams, crs, true); OmsVectorizer vectorizer = new OmsVectorizer(); vectorizer.pm = pm; vectorizer.inRaster = inCoverage; vectorizer.pValue = 2.0; vectorizer.pThres = 1; vectorizer.fDefault = "rast"; vectorizer.process(); SimpleFeatureCollection outGeodata = vectorizer.outVector; assertEquals(2, outGeodata.size()); List<SimpleFeature> features = FeatureUtilities.featureCollectionToList(outGeodata); SimpleFeature f1 = features.get(0); SimpleFeature f2 = features.get(1); Geometry g1 = (Geometry) f1.getDefaultGeometry(); Geometry g2 = (Geometry) f2.getDefaultGeometry(); // SimpleFeature nvFeature = f1; SimpleFeature valuesFeature = f2; if (g1.getArea() < g2.getArea()) { // nvFeature = f2; valuesFeature = f1; } double value = ((Number) valuesFeature.getAttribute("rast")).doubleValue(); assertEquals(2.0, value, 0.0000001); Geometry geometry = (Geometry) valuesFeature.getDefaultGeometry(); double area = geometry.getArea(); assertEquals(6300.0, area, 0.0000001); }
Example 5
Source File: TestVectorizer.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
public void testVectorizer2() throws Exception { double[][] inData = HMTestMaps.extractNet0Data; HashMap<String, Double> envelopeParams = HMTestMaps.getEnvelopeparams(); CoordinateReferenceSystem crs = HMTestMaps.getCrs(); GridCoverage2D inCoverage = CoverageUtilities.buildCoverage("data", inData, envelopeParams, crs, true); OmsVectorizer vectorizer = new OmsVectorizer(); vectorizer.pm = pm; vectorizer.inRaster = inCoverage; vectorizer.pValue = null; vectorizer.pThres = 1; vectorizer.fDefault = "rast"; vectorizer.process(); SimpleFeatureCollection outGeodata = vectorizer.outVector; assertEquals(2, outGeodata.size()); List<SimpleFeature> features = FeatureUtilities.featureCollectionToList(outGeodata); SimpleFeature f1 = features.get(0); SimpleFeature f2 = features.get(1); Geometry g1 = (Geometry) f1.getDefaultGeometry(); Geometry g2 = (Geometry) f2.getDefaultGeometry(); // SimpleFeature nvFeature = f1; SimpleFeature valuesFeature = f2; if (g1.getArea() < g2.getArea()) { // nvFeature = f2; valuesFeature = f1; } double value = ((Number) valuesFeature.getAttribute("rast")).doubleValue(); assertEquals(2.0, value, 0.0000001); Geometry geometry = (Geometry) valuesFeature.getDefaultGeometry(); double area = geometry.getArea(); assertEquals(6300.0, area, 0.0000001); }
Example 6
Source File: TestGeometryUtilities.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
public void testPolygonToUnitScaler() throws Exception { WKTReader reader = new WKTReader(); Geometry geometry = reader.read(IRREGULAR_POLYGON); Geometry scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry); double area = scaled.getArea(); assertEquals(1.0, area, DELTA); geometry = reader.read(TWO_BALLS); scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry); area = scaled.getArea(); assertEquals(1.0, area, DELTA); geometry = reader.read(RECTANGLE); scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry); area = scaled.getArea(); assertEquals(1.0, area, DELTA); double perim = scaled.getLength(); assertEquals(4.0, perim, DELTA); // hexa geometry = reader.read(HEXAGON); scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry); area = scaled.getArea(); assertEquals(1.0, area, DELTA); perim = scaled.getLength(); assertEquals(3.72241943, perim, DELTA); // triangle geometry = reader.read(EQUILATERAL_TRIANGLE); scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry); area = scaled.getArea(); assertEquals(1.0, area, DELTA); perim = scaled.getLength(); assertEquals(4.55901411, perim, DELTA); }
Example 7
Source File: PolygonAreaCalculator.java From geowave with Apache License 2.0 | 5 votes |
public double getAreaSimple(final Geometry polygon) throws Exception { final Point centroid = polygon.getCentroid(); final CoordinateReferenceSystem equalAreaCRS = lookupUtmCrs(centroid.getY(), centroid.getX()); final MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, equalAreaCRS, true); final Geometry transformedPolygon = JTS.transform(polygon, transform); return transformedPolygon.getArea() * SQM_2_SQKM; }
Example 8
Source File: PolygonAreaCalculator.java From geowave with Apache License 2.0 | 5 votes |
public double getAreaDensify(final Geometry polygon) throws Exception { final Point centroid = polygon.getCentroid(); final CoordinateReferenceSystem equalAreaCRS = lookupUtmCrs(centroid.getY(), centroid.getX()); final double vertexSpacing = polygon.getLength() / densifyVertexCount; final Geometry densePolygon = Densifier.densify(polygon, vertexSpacing); final MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, equalAreaCRS, true); final Geometry transformedPolygon = JTS.transform(densePolygon, transform); return transformedPolygon.getArea() * SQM_2_SQKM; }
Example 9
Source File: ExtractGeometryFilterVisitor.java From geowave with Apache License 2.0 | 5 votes |
@Override public Object visit(final And filter, final Object data) { ExtractGeometryFilterVisitorResult finalResult = null; for (final Filter f : filter.getChildren()) { final Object obj = f.accept(this, data); if ((obj != null) && (obj instanceof ExtractGeometryFilterVisitorResult)) { final ExtractGeometryFilterVisitorResult currentResult = (ExtractGeometryFilterVisitorResult) obj; final Geometry currentGeom = currentResult.getGeometry(); final double currentArea = currentGeom.getArea(); if (finalResult == null) { finalResult = currentResult; } else if (!Double.isInfinite(currentArea) && !Double.isNaN(currentArea)) { // if predicates match then we can combine the geometry as // well as predicate if (currentResult.matchPredicate(finalResult)) { finalResult = new ExtractGeometryFilterVisitorResult( finalResult.getGeometry().intersection(currentGeom), currentResult.getCompareOp()); } else { // if predicate doesn't match then still combine // geometry but set predicate to null finalResult = new ExtractGeometryFilterVisitorResult( finalResult.getGeometry().intersection(currentGeom), null); } } else { finalResult = new ExtractGeometryFilterVisitorResult(finalResult.getGeometry(), null); } } } return finalResult; }
Example 10
Source File: ExtractGeometryFilterVisitor.java From geowave with Apache License 2.0 | 5 votes |
@Override public Object visit(final Or filter, final Object data) { ExtractGeometryFilterVisitorResult finalResult = new ExtractGeometryFilterVisitorResult( new GeometryFactory().toGeometry(new Envelope()), null); for (final Filter f : filter.getChildren()) { final Object obj = f.accept(this, data); if ((obj != null) && (obj instanceof ExtractGeometryFilterVisitorResult)) { final ExtractGeometryFilterVisitorResult currentResult = (ExtractGeometryFilterVisitorResult) obj; final Geometry currentGeom = currentResult.getGeometry(); final double currentArea = currentGeom.getArea(); if (finalResult.getGeometry().isEmpty()) { finalResult = currentResult; } else if (!Double.isInfinite(currentArea) && !Double.isNaN(currentArea)) { if (currentResult.matchPredicate(finalResult)) { finalResult = new ExtractGeometryFilterVisitorResult( finalResult.getGeometry().union(currentGeom), currentResult.getCompareOp()); } else { finalResult = new ExtractGeometryFilterVisitorResult( finalResult.getGeometry().union(currentGeom), null); } } else { finalResult = new ExtractGeometryFilterVisitorResult(finalResult.getGeometry(), null); } } } if (finalResult.getGeometry().isEmpty()) { return new ExtractGeometryFilterVisitorResult(infinity(), null); } return finalResult; }
Example 11
Source File: OmsMeltonNumber.java From hortonmachine with GNU General Public License v3.0 | 4 votes |
@Execute public void process() throws Exception { checkNull(inElev, inFans); RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev); int cols = regionMap.getCols(); int rows = regionMap.getRows(); double west = regionMap.getWest(); double east = regionMap.getEast(); double south = regionMap.getSouth(); double north = regionMap.getNorth(); AttributeType type = inFans.getSchema().getType(fId); if (type == null) { throw new ModelsIllegalargumentException(MessageFormat.format("The attribute {0} does not exist in the vector map.", fId), this, pm); } List<SimpleFeature> fansList = FeatureUtilities.featureCollectionToList(inFans); outMelton = new String[fansList.size()][2]; int index = 0; pm.beginTask("Calculating Melton number for fans...", fansList.size()); for( SimpleFeature fan : fansList ) { Object attribute = fan.getAttribute(fId); // rasterize the fan DefaultFeatureCollection newCollection = new DefaultFeatureCollection(); newCollection.add(fan); OmsScanLineRasterizer rasterizer = new OmsScanLineRasterizer(); rasterizer.inVector = newCollection; rasterizer.pCols = cols; rasterizer.pRows = rows; rasterizer.pNorth = north; rasterizer.pSouth = south; rasterizer.pEast = east; rasterizer.pWest = west; rasterizer.pValue = 1.0; rasterizer.pm = new DummyProgressMonitor(); rasterizer.process(); GridCoverage2D rasterizedFan = rasterizer.outRaster; GridCoverage2D fanElev = CoverageUtilities.coverageValuesMapper(inElev, rasterizedFan); // extract min and max OmsRasterSummary summary = new OmsRasterSummary(); summary.pm = new DummyProgressMonitor(); summary.inRaster = fanElev; summary.process(); double min = summary.outMin; double max = summary.outMax; // get the suface of the fan Geometry geometry = (Geometry) fan.getDefaultGeometry(); double area = geometry.getArea(); // calculate Melton double melton = (max - min) / sqrt(area); outMelton[index][0] = attribute.toString(); outMelton[index][1] = String.valueOf(melton); index++; pm.message(MessageFormat.format("id: {0} gave Melton number: {1}", attribute.toString(), melton)); pm.message("Based on max: " + max + " min: " + min + " and area: " + area); pm.worked(1); } pm.done(); }
Example 12
Source File: VectorTileEncoder.java From java-vector-tile with Apache License 2.0 | 4 votes |
/** * Add a feature with layer name (typically feature type name), some attributes * and a Geometry. The Geometry must be in "pixel" space 0,0 upper left and * 256,256 lower right. * <p> * For optimization, geometries will be clipped and simplified. Features with * geometries outside of the tile will be skipped. * * @param layerName * @param attributes * @param geometry * @param id */ public void addFeature(String layerName, Map<String, ?> attributes, Geometry geometry, long id) { // skip small Polygon/LineString. if (geometry instanceof MultiPolygon && geometry.getArea() < minimumArea) { return; } if (geometry instanceof Polygon && geometry.getArea() < minimumArea) { return; } if (geometry instanceof LineString && geometry.getLength() < minimumLength) { return; } // special handling of GeometryCollection. subclasses are not handled here. if (geometry.getClass().equals(GeometryCollection.class)) { for (int i = 0; i < geometry.getNumGeometries(); i++) { Geometry subGeometry = geometry.getGeometryN(i); // keeping the id. any better suggestion? addFeature(layerName, attributes, subGeometry, id); } return; } // clip geometry if (geometry instanceof Point) { if (!clipCovers(geometry)) { return; } } else { geometry = clipGeometry(geometry); } // simplify non-points if (simplificationDistanceTolerance > 0.0 && !(geometry instanceof Point)) { geometry = TopologyPreservingSimplifier.simplify(geometry, simplificationDistanceTolerance); } // no need to add empty geometry if (geometry.isEmpty()) { return; } Layer layer = layers.get(layerName); if (layer == null) { layer = new Layer(); layers.put(layerName, layer); } Feature feature = new Feature(); feature.geometry = geometry; feature.id = id; this.autoincrement = Math.max(this.autoincrement, id + 1); for (Map.Entry<String, ?> e : attributes.entrySet()) { // skip attribute without value if (e.getValue() == null) { continue; } feature.tags.add(layer.key(e.getKey())); feature.tags.add(layer.value(e.getValue())); } layer.features.add(feature); }