private SimpleFeatureCollection makePointFeatures() throws Exception { Map<Vertex, Double> points = makePoints(); /* Stage the point features in memory */ DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(null, pointSchema); SimpleFeatureBuilder fbuilder = new SimpleFeatureBuilder(pointSchema); GeometryFactory gf = new GeometryFactory(); for (Map.Entry<Vertex, Double> entry : points.entrySet()) { Vertex vertex = entry.getKey(); Double travelTime = entry.getValue(); fbuilder.add(gf.createPoint(vertex.getCoordinate())); fbuilder.add(travelTime); featureCollection.add(fbuilder.buildFeature(null)); } return featureCollection; }
if (geoJsonGeom instanceof org.geojson.Point) { org.geojson.Point geoJsonPoint = (org.geojson.Point) geoJsonGeom; return gf.createPoint(new Coordinate(geoJsonPoint.getCoordinates().getLongitude(), geoJsonPoint .getCoordinates().getLatitude())); LinearRing shell = gf.createLinearRing(convertPath(geoJsonPolygon.getExteriorRing())); LinearRing[] holes = new LinearRing[geoJsonPolygon.getInteriorRings().size()]; int i = 0; for (List<LngLatAlt> hole : geoJsonPolygon.getInteriorRings()) { holes[i++] = gf.createLinearRing(convertPath(hole)); return gf.createPolygon(shell, holes); jtsPolygons[i++] = (Polygon) convertGeoJsonToJtsGeometry(geoJsonPoly); return gf.createMultiPolygon(jtsPolygons); return gf.createLineString(convertPath(geoJsonLineString.getCoordinates())); jtsLineStrings[i++] = (LineString) convertGeoJsonToJtsGeometry(geoJsonLineString); return gf.createMultiLineString(jtsLineStrings);
final CoordinateSequence coordSeq = geomFactory.getCoordinateSequenceFactory().create(cmdLength, 2); int coordIndex = 0; Coordinate nextCoord; nextCoord.setOrdinate(0, cursor.x); nextCoord.setOrdinate(1, cursor.y); return coordSeq.size() == 1 ? geomFactory.createPoint(coordSeq) : geomFactory.createMultiPoint(coordSeq);
/** * Creates a circle shape, using the JTS buffer algorithm. The method is used when there is no street found within the given traveltime, e.g. when * the pointer is placed on a field or in the woods.<br> * TODO: Note it is actually not correct to do buffer calculation in Euclidian 2D, since the resulting shape will be elliptical when projected. * * @param dropPoint the location given by the user * @param pathToStreet the path from the dropPoint to the street, used to retrieve the buffer distance * @return a Circle */ private Geometry createCirle(Coordinate dropPoint, LineString pathToStreet) { double length = pathToStreet.getLength(); GeometryFactory gf = new GeometryFactory(); Point dp = gf.createPoint(dropPoint); Geometry buffer = dp.buffer(length); return buffer; }
final GeometryFactory gf = new GeometryFactory(); points.add(new Coordinate(-10, -10)); points.add(new Coordinate(-10, 10)); points.add(new Coordinate(10, 10)); points.add(new Coordinate(10, -10)); points.add(new Coordinate(-10, -10)); final Polygon polygon = gf.createPolygon(new LinearRing(new CoordinateArraySequence(points .toArray(new Coordinate[points.size()])), gf), null); final Coordinate coord = new Coordinate(0, 0); final Point point = gf.createPoint(coord);
public void testExecutePoint() throws Exception { SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setName("featureType"); tb.add("geometry", Geometry.class); tb.add("integer", Integer.class); GeometryFactory gf = new GeometryFactory(); SimpleFeatureBuilder b = new SimpleFeatureBuilder(tb.buildFeatureType()); DefaultFeatureCollection features = new DefaultFeatureCollection(null, b.getFeatureType()); for (int i = 0; i < 2; i++) { b.add(gf.createPoint(new Coordinate(i, i))); b.add(i); features.add(b.buildFeature(i + "")); } Double distance = new Double(500); BufferFeatureCollection process = new BufferFeatureCollection(); SimpleFeatureCollection output = process.execute(features, distance, null); assertEquals(2, output.size()); SimpleFeatureIterator iterator = output.features(); for (int i = 0; i < 2; i++) { Geometry expected = gf.createPoint(new Coordinate(i, i)).buffer(distance); SimpleFeature sf = iterator.next(); assertTrue(expected.equals((Geometry) sf.getDefaultGeometry())); } }
@Test public void testShapeFileWriterWithSelfCreatedContent() throws IOException { String outFile = utils.getOutputDirectory() + "/test.shp"; SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder(); b.setName("EvacuationArea"); b.setCRS(DefaultGeographicCRS.WGS84); b.add("the_geom", MultiPolygon.class); b.add("name", String.class); SimpleFeatureType ft = b.buildFeatureType(); GeometryFactory geofac = new GeometryFactory(); LinearRing lr = geofac.createLinearRing(new Coordinate[]{new Coordinate(0,0),new Coordinate(0,1),new Coordinate(1,1),new Coordinate(0,0)}); Polygon p = geofac.createPolygon(lr,null); MultiPolygon mp = geofac.createMultiPolygon(new Polygon[]{p}); Collection<SimpleFeature> features = new ArrayList<SimpleFeature>(); features.add(SimpleFeatureBuilder.build(ft, new Object[]{mp,"test_name"},"fid")); Geometry g0 = (Geometry) features.iterator().next().getDefaultGeometry(); SimpleFeatureIterator it1 = fts1.features(); SimpleFeature ft1 = it1.next(); Geometry g1 = (Geometry) ft1.getDefaultGeometry(); Assert.assertEquals(g0.getCoordinates().length, g1.getCoordinates().length);
public void testBounds() throws Exception { PrecisionModel pm = new PrecisionModel(); Geometry[] g = new Geometry[4]; GeometryFactory gf = new GeometryFactory(pm); 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); SimpleFeatureType t = tb.buildFeatureType(); SimpleFeatureCollection fc = FeatureCollections.newCollection(); SimpleFeatureBuilder b = new SimpleFeatureBuilder(t); for (int i = 0; i < g.length; i++) { b.add(g[i]); fc.add(b.buildFeature(null)); } assertEquals(gc.getEnvelopeInternal(), fc.getBounds()); }
if (envelope.isNull()) { return createPoint((CoordinateSequence)null); if (envelope.getMinX() == envelope.getMaxX() && envelope.getMinY() == envelope.getMaxY()) { return createPoint(new Coordinate(envelope.getMinX(), envelope.getMinY())); if (envelope.getMinX() == envelope.getMaxX() || envelope.getMinY() == envelope.getMaxY()) { return createLineString(new Coordinate[]{ new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMaxX(), envelope.getMaxY()) }); return createPolygon(createLinearRing(new Coordinate[]{ new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMinX(), envelope.getMaxY()),
public void testBuffer() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setCRS(null); builder.add("geometry", Polygon.class); builder.add("distance", Float.class); builder.setName("schema"); coords[0] = new Coordinate(0, 0); coords[1] = new Coordinate(10, 0); coords[2] = new Coordinate(10, 10); coords[3] = new Coordinate(0, 10); coords[4] = new Coordinate(0, 0); GeometryFactory gf = new GeometryFactory(new PrecisionModel()); LinearRing ring = gf.createLinearRing(coords); attributes[0] = gf.createPolygon(ring, null); attributes[1] = new Float(100.0); SimpleFeature feature = SimpleFeatureBuilder.build(schema, attributes, null); Object result = buffer.evaluate(feature); assertTrue(result instanceof Polygon); assertTrue(((Polygon) result).getArea() > 35314);
/** * Creates an elliptical {@link Polygon}. * If the supplied envelope is square the * result will be a circle. * * @return an ellipse or circle */ public Polygon createEllipse() { Envelope env = dim.getEnvelope(); double xRadius = env.getWidth() / 2.0; double yRadius = env.getHeight() / 2.0; double centreX = env.getMinX() + xRadius; double centreY = env.getMinY() + yRadius; Coordinate[] pts = new Coordinate[nPts + 1]; int iPt = 0; for (int i = 0; i < nPts; i++) { double ang = i * (2 * Math.PI / nPts); double x = xRadius * Math.cos(ang) + centreX; double y = yRadius * Math.sin(ang) + centreY; pts[iPt++] = coord(x, y); } pts[iPt] = new Coordinate(pts[0]); LinearRing ring = geomFact.createLinearRing(pts); Polygon poly = geomFact.createPolygon(ring, null); return (Polygon) rotate(poly); } /**
GeometryFactory gf = new GeometryFactory(); Coordinate dropPoint = new Coordinate(lon, lat); pathToStreetCoords[0] = dropPoint; pathToStreetCoords[1] = origin.getCoordinate(); LineString pathToStreet = gf.createLineString(pathToStreetCoords); geometryJSON.write(gf.createMultiPoint(coords), sw); LOG.debug("done"); } else if (output.equals(SIsochrone.RESULT_TYPE_SHED)) { geomsArray = new Geometry[coords.length]; for (int j = 0; j < geomsArray.length; j++) { geomsArray[j] = gf.createPoint(coords[j]); GeometryCollection gc = gf.createGeometryCollection(geomsArray); edges[k++] = ls; LOG.debug("create multilinestring from {} geoms", edges.length); mls = gf.createMultiLineString(edges); LOG.debug("write geom"); geometryJSON.write(mls, sw); Geometry mls = gf.createMultiLineString(edges); LOG.debug("write debug geom"); geometryJSON.write(mls, sw);
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder(); b.setName( "LineFeature" ); b.setCRS( DefaultGeographicCRS.WGS84 ); // set crs first b.add( "line", LineString.class ); // then add geometry double lonEnd = 9.4; coordinates[0] = new Coordinate(lonStart, latStart); coordinates[1] = new Coordinate(lonEnd, latEnd); LineString line = gFac.createLineString(coordinates ); featureBuilderLines.add(line); SimpleFeature featureLine = featureBuilderLines.buildFeature(null); collectionLines.add(featureLine);
int nSide = nPts / 4; if (nSide < 1) nSide = 1; double XsegLen = dim.getEnvelope().getWidth() / nSide; double YsegLen = dim.getEnvelope().getHeight() / nSide; double x = env.getMinX() + i * XsegLen; double y = env.getMinY(); pts[ipt++] = coord(x, y); pts[ipt++] = coord(x, y); pts[ipt++] = new Coordinate(pts[0]); LinearRing ring = geomFact.createLinearRing(pts); Polygon poly = geomFact.createPolygon(ring, null); return (Polygon) rotate(poly);
public SimpleFeature createPoint(final Coord coordinate, final Object[] attributeValues, final String id) { Point p = this.fac.createPoint(new Coordinate(coordinate.getX(), coordinate.getY())); this.builder.add(p); for (int i = 0; i < attributeValues.length; i++) { Object value = attributeValues[i]; this.builder.add(value); } return this.builder.buildFeature(id); }
static public Polygon toPolygon(Envelope env, int SRID) { Coordinate[] coords = new Coordinate[5]; coords[0] = new Coordinate(env.getMinX(), env.getMinY()); coords[1] = new Coordinate(env.getMinX(), env.getMaxY()); coords[2] = new Coordinate(env.getMaxX(), env.getMaxY()); coords[3] = new Coordinate(env.getMaxX(), env.getMinY()); coords[4] = new Coordinate(env.getMinX(), env.getMinY()); LinearRing shell = geomFactory.createLinearRing(coords); Polygon pg = geomFactory.createPolygon(shell, null); pg.setSRID(SRID); return pg; }
protected LineString horizontalBisector(Geometry geometry) { Envelope envelope = geometry.getEnvelopeInternal(); // Assert: for areas, minx <> maxx double avgY = avg(envelope.getMinY(), envelope.getMaxY()); return factory.createLineString(new Coordinate[] { new Coordinate(envelope.getMinX(), avgY), new Coordinate(envelope.getMaxX(), avgY) }); }
GeometryFactory geomFactory = new GeometryFactory(); Coordinate[] coordinates = new Coordinate[] { edge.getFromVertex().getCoordinate(), edge.getToVertex().getCoordinate() }; edgeGeom = GeometryUtils.getGeometryFactory().createLineString(coordinates); hasGeom = false; OffsetCurveBuilder offsetBuilder = new OffsetCurveBuilder(new PrecisionModel(), bufParams); Coordinate[] coords = offsetBuilder.getOffsetCurve(midLineGeom.getCoordinates(), lineWidth * 0.4); if (coords.length < 2) continue; // Can happen for very small edges (<1mm) LineString offsetLine = geomFactory.createLineString(coords); Shape midLineShape = shapeWriter.toShape(midLineGeom); Shape offsetShape = shapeWriter.toShape(offsetLine); vvAttrs.color = null; vvAttrs.label = null; Point point = geomFactory.createPoint(new Coordinate(vertex.getLon(), vertex.getLat())); boolean render = evRenderer.renderVertex(vertex, vvAttrs); if (!render) context.graphics.draw(shape); if (vvAttrs.label != null && lineWidth > 6.0f && context.bbox.contains(point.getCoordinate())) { context.graphics.setColor(Color.BLACK); int labelWidth = largeFontMetrics.stringWidth(vvAttrs.label);
@Test public void testCreatePlainFeature() { SimpleFeatureType sft = PlainFeatureFactory.createPlainFeatureType("MyPoint", Point.class, DefaultGeographicCRS.WGS84); final GeometryFactory gf = new GeometryFactory(); final Point point = gf.createPoint(new Coordinate(0.5, 0.6)); final SimpleFeature feature1 = PlainFeatureFactory.createPlainFeature(sft, "_1", point, "fill:#0033AA"); assertEquals("_1", feature1.getID()); assertEquals(point, feature1.getDefaultGeometry()); assertEquals(point, feature1.getAttribute(PlainFeatureFactory.ATTRIB_NAME_GEOMETRY)); assertEquals("fill:#0033AA", feature1.getAttribute(PlainFeatureFactory.ATTRIB_NAME_STYLE_CSS)); SimpleFeature feature2 = PlainFeatureFactory.createPlainFeature(sft, "_2", null, "fill:#0033AA"); assertNotNull(feature2.getDefaultGeometry()); assertTrue(gf.createPoint(new Coordinate()).compareTo(feature2.getDefaultGeometry()) == 0); final SimpleFeature feature3 = PlainFeatureFactory.createPlainFeature(sft, "_3", point, null); assertEquals(null, feature3.getAttribute(PlainFeatureFactory.ATTRIB_NAME_STYLE_CSS)); } }
@Override public String resolve(double x, double y) { System.out.println("x="+x+", y="+y); FeatureIterator<Feature> iterator = collection.features(); while( iterator.hasNext() ){ SimpleFeature feature = (SimpleFeature) iterator.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate coord = new Coordinate(x, y); Point point = geometryFactory.createPoint(coord); //System.out.println("checking "+point.toString()); if(geom.contains(point)) { return feature.getAttribute(this.nameField).toString(); } } return null; }