@SqlNullable @Description("Returns a float between 0 and 1 representing the location of the closest point on the LineString to the given Point, as a fraction of total 2d line length.") @ScalarFunction("line_locate_point") @SqlType(DOUBLE) public static Double lineLocatePoint(@SqlType(GEOMETRY_TYPE_NAME) Slice lineSlice, @SqlType(GEOMETRY_TYPE_NAME) Slice pointSlice) { Geometry line = JtsGeometrySerde.deserialize(lineSlice); Geometry point = JtsGeometrySerde.deserialize(pointSlice); if (line.isEmpty() || point.isEmpty()) { return null; } GeometryType lineType = GeometryType.getForJtsGeometryType(line.getGeometryType()); if (lineType != GeometryType.LINE_STRING && lineType != GeometryType.MULTI_LINE_STRING) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("First argument to line_locate_point must be a LineString or a MultiLineString. Got: %s", line.getGeometryType())); } GeometryType pointType = GeometryType.getForJtsGeometryType(point.getGeometryType()); if (pointType != GeometryType.POINT) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Second argument to line_locate_point must be a Point. Got: %s", point.getGeometryType())); } return new LengthIndexedLine(line).indexOf(point.getCoordinate()) / line.getLength(); }
public Coordinate getCoordinate() { return geometry.getCoordinate(); }
public Number getX(int series, int item) { // TODO: return the centroid return geometries.get(series).getCoordinate().x; }
public Number getY(int series, int item) { return geometries.get(series).getCoordinate().x; }
/** * Return D as defined by SDO_GTYPE (either 2,3 or 4). * * <p>For normal JTS Geometry this will be 2 or 3 depending if geom.getCoordinate.z is * Double.NaN. * * <p>Subclasses may override as required. * * @param geom * @return <code>3</code> */ public static int D(Geometry geom) { CoordinateSequenceFactory f = geom.getFactory().getCoordinateSequenceFactory(); if (f instanceof CoordinateAccessFactory) { return ((CoordinateAccessFactory) f).getDimension(); } else if (geom == null || geom.getCoordinate() == null || geom.isEmpty()) { return 2; } else { // return 3; return Double.isNaN(geom.getCoordinate().z) ? 2 : 3; } }
/** * Gets a point to represent the Geometry. If the Geometry is a point, this is returned. * Otherwise, the centroid is used. * * @param g the geometry to find a point for * @return a point representing the Geometry */ private static Coordinate getPoint(Geometry g) { if (g.getNumPoints() == 1) return g.getCoordinate(); return g.getCentroid().getCoordinate(); }
/** * Gets a point to represent the Geometry. If the Geometry is a point, this is returned. * Otherwise, the centroid is used. * * @param g the geometry to find a point for * @return a point representing the Geometry */ private static Coordinate getRepresentativePoint(Geometry g) { if (g.getNumPoints() == 1) return g.getCoordinate(); return g.getCentroid().getCoordinate(); }
Coordinate c = (mp.getGeometryN(t)).getCoordinate(); buffer.putDouble(c.x); buffer.putDouble(c.y); Coordinate c = (mp.getGeometryN(t)).getCoordinate(); double z = c.z;
private static Geometry pointInGeometry(Geometry g) { Point p = g.getCentroid(); if (g instanceof Polygon) { // if the geometry is heavily generalized centroid computation may fail and return NaN if (Double.isNaN(p.getX()) || Double.isNaN(p.getY())) return g.getFactory().createPoint(g.getCoordinate()); // otherwise let's check if the point is inside. Again, this check and // "getInteriorPoint" // will work only if the geometry is valid if (g.isValid() && !g.contains(p)) { try { p = g.getInteriorPoint(); } catch (Exception e) { // generalized geometries might make interior point go bye bye return p; } } else { return p; } } return p; }
Coordinate p = geom.getCoordinate(); srcPt[0] = p.x; srcPt[1] = p.y;
/** * Finds the centroid of the input geometry if input = point, line, polygon --> return a point * that represents the centroid of that geom if input = geometry collection --> return a * multipoint that represents the centoid of each sub-geom * * @param g */ public static Geometry getCentroid(Geometry g) { if (g instanceof Point || g instanceof MultiPoint) { return g; } else if (g instanceof GeometryCollection) { final GeometryCollection gc = (GeometryCollection) g; final Coordinate[] pts = new Coordinate[gc.getNumGeometries()]; final int length = gc.getNumGeometries(); for (int t = 0; t < length; t++) { pts[t] = pointInGeometry(gc.getGeometryN(t)).getCoordinate(); } return g.getFactory().createMultiPoint(pts); } else if (g != null) { return pointInGeometry(g); } return null; }
@Test public void multiPointWithInnerParens() throws Exception { String WKT = "MULTIPOINT ((111 -47), (110 -46.5))"; WKTReader reader = new WKTReader2(); Geometry geometry = reader.read(WKT); assertNotNull(geometry); assertTrue(geometry instanceof MultiPoint); MultiPoint mp = (MultiPoint) geometry; assertEquals(2, mp.getNumGeometries()); assertEquals(new Coordinate(111, -47), mp.getGeometryN(0).getCoordinate()); assertEquals(new Coordinate(110, -46.5), mp.getGeometryN(1).getCoordinate()); }
@Test public void multiPoint() throws Exception { String WKT = "MULTIPOINT (111 -47, 110 -46.5)"; WKTReader reader = new WKTReader2(); Geometry geometry = reader.read(WKT); assertNotNull(geometry); assertTrue(geometry instanceof MultiPoint); MultiPoint mp = (MultiPoint) geometry; assertEquals(2, mp.getNumGeometries()); assertEquals(new Coordinate(111, -47), mp.getGeometryN(0).getCoordinate()); assertEquals(new Coordinate(110, -46.5), mp.getGeometryN(1).getCoordinate()); }
@Test public void testToGeographic() throws Exception { String wkt = "GEOGCS[\"GDA94\"," + " DATUM[\"Geocentric Datum of Australia 1994\"," + " SPHEROID[\"GRS 1980\", 6378137.0, 298.257222101, AUTHORITY[\"EPSG\",\"7019\"]]," + " TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], " + " AUTHORITY[\"EPSG\",\"6283\"]], " + " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]]," + " UNIT[\"degree\", 0.017453292519943295], " + " AXIS[\"Geodetic longitude\", EAST], " + " AXIS[\"Geodetic latitude\", NORTH], " + " AXIS[\"Ellipsoidal height\", UP], " + " AUTHORITY[\"EPSG\",\"4939\"]]"; CoordinateReferenceSystem gda94 = CRS.parseWKT(wkt); GeometryFactory gf = new GeometryFactory(); Point point = gf.createPoint(new Coordinate(130.875825803896, -16.4491956225999, 0.0)); Geometry worldPoint = JTS.toGeographic(point, gda94); assertTrue(worldPoint instanceof Point); assertEquals(point.getX(), worldPoint.getCoordinate().x, 0.00000001); }
public void testDifferentCRS() throws Exception { CoordinateReferenceSystem srcCRS = DefaultGeographicCRS.WGS84; GeometryFactory fac = new GeometryFactory(); Point p = fac.createPoint(new Coordinate(10, 10)); SimpleFeatureCollection features = createTestFeatureCollection(srcCRS, p); FeatureReader<SimpleFeatureType, SimpleFeature> original = DataUtilities.reader(features); CoordinateReferenceSystem destCRS = DefaultEngineeringCRS.CARTESIAN_2D; try (ForceCoordinateSystemFeatureReader modified = new ForceCoordinateSystemFeatureReader(DataUtilities.reader(features), destCRS); ) { SimpleFeature f1 = original.next(); SimpleFeature f2 = modified.next(); assertEquals( ((Geometry) f1.getDefaultGeometry()).getCoordinate(), ((Geometry) f2.getDefaultGeometry()).getCoordinate()); SimpleFeatureType f1Type = f1.getFeatureType(); SimpleFeatureType f2Type = f2.getFeatureType(); assertFalse( f1Type.getCoordinateReferenceSystem() .equals(f2Type.getCoordinateReferenceSystem())); assertEquals(srcCRS, f1Type.getCoordinateReferenceSystem()); assertEquals(srcCRS, f1Type.getGeometryDescriptor().getCoordinateReferenceSystem()); assertEquals(destCRS, f2Type.getCoordinateReferenceSystem()); assertEquals(destCRS, f2Type.getGeometryDescriptor().getCoordinateReferenceSystem()); assertFalse(original.hasNext()); assertFalse(modified.hasNext()); assertNotNull(modified.builder); } }
public void testBounds() throws Exception { GeometryFactory gf = new GeometryFactory(); Geometry[] g = new Geometry[4]; g[0] = gf.createPoint(new Coordinate(0, 0)); g[1] = gf.createPoint(new Coordinate(0, 10)); g[2] = gf.createPoint(new Coordinate(10, 0)); g[3] = gf.createPoint(new Coordinate(10, 10)); GeometryCollection gc = gf.createGeometryCollection(g); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setName("bounds"); tb.setCRS(null); tb.add("p1", Point.class); tb.add("p2", Point.class); tb.add("p3", Point.class); tb.add("p4", Point.class); SimpleFeatureType t = tb.buildFeatureType(); SimpleFeature f = SimpleFeatureBuilder.build(t, g, null); assertEquals(gc.getEnvelopeInternal(), f.getBounds()); g[1].getCoordinate().y = 20; g[2].getCoordinate().x = 20; f.setAttribute(1, g[1]); f.setAttribute(2, g[2]); gc = gf.createGeometryCollection(g); assertEquals(gc.getEnvelopeInternal(), f.getBounds()); }
((Geometry) f1.getDefaultGeometry()).getCoordinate(), ((Geometry) f2.getDefaultGeometry()).getCoordinate()); SimpleFeatureType f1Type = f1.getFeatureType(); SimpleFeatureType f2Type = f2.getFeatureType();
public void testDifferentCRS() throws Exception { CoordinateReferenceSystem srcCRS = DefaultGeographicCRS.WGS84; GeometryFactory fac = new GeometryFactory(); Point p = fac.createPoint(new Coordinate(10, 10)); SimpleFeatureCollection collection = createDatastore(srcCRS, p); SimpleFeatureIterator original = collection.features(); CoordinateReferenceSystem destCRS = DefaultEngineeringCRS.CARTESIAN_2D; ForceCoordinateSystemIterator modified = new ForceCoordinateSystemIterator( collection.features(), collection.getSchema(), destCRS); SimpleFeature f1 = original.next(); SimpleFeature f2 = modified.next(); assertEquals( ((Geometry) f1.getDefaultGeometry()).getCoordinate(), ((Geometry) f2.getDefaultGeometry()).getCoordinate()); assertFalse( f1.getFeatureType() .getCoordinateReferenceSystem() .equals(f2.getFeatureType().getCoordinateReferenceSystem())); assertEquals(srcCRS, f1.getFeatureType().getCoordinateReferenceSystem()); assertEquals( srcCRS, f1.getFeatureType().getGeometryDescriptor().getCoordinateReferenceSystem()); assertEquals(destCRS, f2.getFeatureType().getCoordinateReferenceSystem()); assertEquals( destCRS, f2.getFeatureType().getGeometryDescriptor().getCoordinateReferenceSystem()); assertFalse(original.hasNext()); assertFalse(modified.hasNext()); assertNotNull(modified.builder); }
((Geometry) f1.getDefaultGeometry()).getCoordinate(), ((Geometry) f2.getDefaultGeometry()).getCoordinate()); assertFalse( f2.getFeatureType()
assertEquals("geom", 2.0, geometry.getCoordinate().y); assertEquals("fid preservation", "fid1", feature1.getID());