/* * This method takes a geometry and translate (move) it along x for tx and along y for ty */ public static void moveGeometry(Geometry a, double tx, double ty) throws MismatchedDimensionException, TransformException { System.out.println("a: " + a.getCentroid() + "," + a.getArea()); AffineTransform affineTransform = AffineTransform.getTranslateInstance(tx, ty); MathTransform mathTransform = new AffineTransform2D(affineTransform); Geometry a1 = JTS.transform(a, mathTransform); System.out.println("a': " + a1.getCentroid() + "," + a1.getArea()); }
public static DelaunayNode[] featureCollectionToNodeArray(SimpleFeatureCollection fc) { SimpleFeatureIterator iter = fc.features(); int size = fc.size(); DelaunayNode[] nodes = new DelaunayNode[size]; int index = 0; while (iter.hasNext()) { SimpleFeature next = iter.next(); Geometry geom = (Geometry) next.getDefaultGeometry(); Point centroid; if (geom instanceof Point) { centroid = (Point) geom; } else { centroid = geom.getCentroid(); } DelaunayNode node = new DelaunayNode(); node.setCoordinate(centroid.getCoordinate()); node.setFeature(next); if (!(arrayContains(node, nodes, index))) { nodes[index] = node; index++; } } DelaunayNode[] trimmed = new DelaunayNode[index]; for (int i = 0; i < index; i++) { trimmed[i] = nodes[i]; } return trimmed; }
/** * Must go places! * * @param selection */ public void showLocation(Object selection) { // selection should be an Feture (of some sort) SimpleFeature feature = (SimpleFeature) selection; Geometry geom = (Geometry) feature.getDefaultGeometry(); Point point = geom.getCentroid(); IMap imap = ApplicationGIS.getActiveMap(); if (imap == ApplicationGIS.NO_MAP) return; CoordinateReferenceSystem world = imap.getViewportModel().getCRS(); CoordinateReferenceSystem wsg84 = DefaultGeographicCRS.WGS84; double buffer = 0.01; // how much of the wgs84 world to see Envelope view = point.buffer(buffer).getEnvelopeInternal(); MathTransform transform; try { transform = CRS.findMathTransform(wsg84, world, true); // relaxed } catch (FactoryException e) { return; // no go } Envelope areaOfInterest; try { areaOfInterest = JTS.transform(view, null, transform, 10); } catch (TransformException e) { return; // no go } // NavigationCommandFactory navigate = NavigationCommandFactory.getInstance(); NavCommand show = new SetViewportBBoxCommand(areaOfInterest, world); imap.sendCommandASync(show); }
/** * Finds the geometric attribute requested by the symbolizer. * * @param feature The victim * @param symbolizer The symbolizer * @param style the resolved style for the specified victim * @return The geometry requested in the symbolizer, or the default geometry if none is specified */ private com.vividsolutions.jts.geom.Geometry findGeometry( SimpleFeature feature, Symbolizer symbolizer) { String geomName = getGeometryPropertyName(symbolizer); // get the geometry com.vividsolutions.jts.geom.Geometry geometry; if (geomName == null || feature.getType().getDescriptor(geomName) == null) { geometry = (Geometry) feature.getDefaultGeometry(); } else { geometry = (com.vividsolutions.jts.geom.Geometry) feature.getAttribute(geomName); } if (geometry == null) { return null; // nothing to see here } // if the symbolizer is a point or text symbolizer generate a suitable // location to place the // point in order to avoid recomputing that location at each rendering // step if ((symbolizer instanceof PointSymbolizer || symbolizer instanceof TextSymbolizer) && !(geometry instanceof Point)) { if (geometry instanceof LineString && !(geometry instanceof LinearRing)) { // use the mid point to represent the point/text symbolizer // anchor Coordinate[] coordinates = geometry.getCoordinates(); Coordinate start = coordinates[0]; Coordinate end = coordinates[1]; Coordinate mid = new Coordinate((start.x + end.x) / 2, (start.y + end.y) / 2); geometry = geometry.getFactory().createPoint(mid); } else { // otherwise use the centroid of the polygon geometry = geometry.getCentroid(); } } return geometry; }
private static boolean nonCollectionToMultiPoint( Geometry geom, Class<? extends Geometry> targetType, Collection<Geometry> result) { if (!(geom instanceof GeometryCollection) && MultiPoint.class == targetType) { result.add(geomBuilder.factory.createMultiPoint(geom.getCentroid().getCoordinates())); return true; } return false; }
/** * return true if Geometry is a point and targetType is a point. Result will be populated with the * new point */ private static boolean nonCollectionToPoint( Geometry geom, Class<? extends Geometry> targetType, Collection<Geometry> result) { if (!(geom instanceof GeometryCollection) && Point.class == targetType) { result.add(geom.getCentroid()); return true; } return false; }
boolean layout(PointLabel label, LabelIndex labels) { String text = label.getText(); Feature f = label.getFeature(); Geometry g = label.getGeometry(); Rule rule = label.getRule(); // get center in screen space Coordinate centroid = g.getCentroid().getCoordinate(); PointF anchor = tx.getWorldToCanvas().map(centroid); // apply offsets anchor.x += rule.number(f, TEXT_DX, 0f); anchor.y += rule.number(f, TEXT_DY, 0f); Paint p = label.get(Paint.class, Paint.class); // compute bounds of this label Rect b = new Rect(); p.getTextBounds(text, 0, text.length(), b); // padding float padding = rule.number(f, TEXT_MIN_PADDING, 0f); if (padding > 0f) { b = expand(b, padding); } // label.setAnchor(new Coordinate(center.x,center.y)); // label.setBox(envelope(rectFromBottomLeft(center, b.width(), b.height()))); RectF c = new RectF(b); tx.getCanvasToWorld().mapRect(c); centroid = tx.getCanvasToWorld().map(anchor); label.setAnchor(centroid); RectF box = null; switch (p.getTextAlign()) { case LEFT: box = rectFromBottomLeft(point(centroid), c.width(), c.height()); break; case CENTER: box = rectFromBottomCenter(point(centroid), c.width(), c.height()); break; case RIGHT: box = rectFromBottomRight(point(centroid), c.width(), c.height()); break; } label.setBox(envelope(box)); return labels.insert(label); }
private Shape createSelectedItemsShape() throws NoninvertibleTransformException { Collection selectedGeos = (getPanel().getSelectionManager().getSelectedItems()); Geometry geo = ((Geometry) selectedGeos.iterator().next()); Geometry[] allGeoms = new Geometry[selectedGeos.size()]; int i = 0; for (Iterator j = selectedGeos.iterator(); j.hasNext(); ) allGeoms[i++] = (Geometry) j.next(); GeometryFactory geoFac = new GeometryFactory(); geo = geoFac.createGeometryCollection(allGeoms); if (centerCoord == null) { centerCoord = geo.getCentroid().getCoordinate(); } return getPanel().getJava2DConverter().toShape(geo); }
/*package*/ void addConnectorLinks() { QuadTree<Node> quadTree = getNodesQuadTree(); NetworkFactory factory = network.getFactory(); /* * Try to use the centroid of the polygon. If that does not lie within * the polygon, use an interior point. */ for (Entry<Integer, SimpleFeature> entry : zonesMap.entrySet()) { int zoneId = entry.getKey(); SimpleFeature zone = entry.getValue(); if (SpecialZones.skipZone(zone)) continue; Geometry polygon = (Geometry) zone.getDefaultGeometry(); Point point = polygon.getCentroid(); if (!polygon.contains(point)) point = polygon.getInteriorPoint(); /* * Convert coordinate from WGS84 to EPSG:28992 (Netherlands Projection) */ Coord wgs84Coord = new Coord(point.getCoordinate().x, point.getCoordinate().y); Coord nodeCoord = ct.transform(wgs84Coord); Id<Node> nodeId = Id.create(String.valueOf(zoneId), Node.class); network.addNode(factory.createNode(nodeId, nodeCoord)); Node networkConnectorNode = quadTree.getClosest(nodeCoord.getX(), nodeCoord.getY()); Id<Node> networkConnectorNodeId = networkConnectorNode.getId(); double length = CoordUtils.calcDistance(nodeCoord, networkConnectorNode.getCoord()); Id<Link> linkFromId = Id.create(String.valueOf(zoneId) + "from", Link.class); Link fromLink = factory.createLink(linkFromId, nodeId, networkConnectorNodeId); fromLink.setLength(length); fromLink.setCapacity(this.connectorCapacity); fromLink.setFreespeed(this.connectorLinkFreeSpeed); network.addLink(fromLink); Id<Link> linkToId = Id.create(String.valueOf(zoneId) + "to", Link.class); Link toLink = factory.createLink(linkToId, networkConnectorNodeId, nodeId); toLink.setLength(length); toLink.setCapacity(this.connectorCapacity); toLink.setFreespeed(this.connectorLinkFreeSpeed); network.addLink(toLink); } }
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { switch (markerFillProperties.getFillStyle()) { case SINGLE_CENTERED_SYMBOL: // case a single marker is used into a polygon shapetype Geometry geom = FConverter.java2d_to_jts(shp); com.vividsolutions.jts.geom.Point centroid = geom.getCentroid(); /* * Hay ocasiones en que jts no puede calcular un centroide y * devuelve NaN (por ejemplo con geometrías poligonales cuyos puntos * tienen todos la misma abscisa y distinta ordenada con tan solo * una diferencia de 1 ó 2 unidades) entonces, en lugar de utilizar * este centroide tomamos el centro del bounds del shp (la geometría * es tan pequeña que consideramos que deben coincidir). */ if (!(Double.isNaN(centroid.getX()) || Double.isNaN(centroid.getY()))) { double centroidX = centroid.getX() + markerFillProperties.getXOffset(); double centroidY = centroid.getY() + markerFillProperties.getYOffset(); FPoint2D p = new FPoint2D(new Point2D.Double(centroidX, centroidY)); markerSymbol.draw(g, affineTransform, p, null); } else { double centroidX = shp.getBounds().getCenterX(); double centroidY = shp.getBounds().getCenterY(); FPoint2D p = new FPoint2D(new Point2D.Double(centroidX, centroidY)); markerSymbol.draw(g, affineTransform, p, null); } break; case GRID_FILL: // case a grid fill is used { Rectangle rClip = null; if (g.getClipBounds() != null) { rClip = (Rectangle) g.getClipBounds().clone(); g.setClip(rClip.x, rClip.y, rClip.width, rClip.height); } g.clip(shp); int size = (int) markerSymbol.getSize(); Rectangle rProv = new Rectangle(); rProv.setFrame(0, 0, size, size); Paint resulPatternFill = null; double xSeparation = markerFillProperties.getXSeparation(); // TODO // apply // CartographicSupport double ySeparation = markerFillProperties.getYSeparation(); // TODO // apply // CartographicSupport double xOffset = markerFillProperties.getXOffset(); double yOffset = markerFillProperties.getYOffset(); BufferedImage sample = null; sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); Graphics2D gAux = sample.createGraphics(); try { markerSymbol.drawInsideRectangle(gAux, gAux.getTransform(), rProv, null); } catch (SymbolDrawingException e) { if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) { try { SymbologyFactory.getWarningSymbol( SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, "", SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) .drawInsideRectangle(gAux, gAux.getTransform(), rProv, null); } catch (SymbolDrawingException e1) { // IMPOSSIBLE TO REACH THIS } } else { // should be unreachable code throw new Error(Messages.getString("symbol_shapetype_mismatch")); } } rProv.setRect(0, 0, rProv.getWidth() + xSeparation, rProv.getHeight() + ySeparation); BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB); gAux = bi.createGraphics(); gAux.drawImage(sample, null, (int) (xSeparation * 0.5), (int) (ySeparation * 0.5)); resulPatternFill = new TexturePaint(bi, rProv); sample = null; gAux.dispose(); g.setColor(null); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.translate(xOffset, -yOffset); g.setPaint(resulPatternFill); g.fill(shp); g.translate(-xOffset, +yOffset); g.setClip(rClip); bi = null; } break; case RANDOM_FILL: { double s = markerSymbol.getSize(); Rectangle r = shp.getBounds(); int drawCount = (int) (Math.min(r.getWidth(), r.getHeight()) / s); Random random = new Random(); int minx = r.x; int miny = r.y; int width = r.width; int height = r.height; r = new Rectangle(); g.setClip(shp); for (int i = 0; (cancel == null || !cancel.isCanceled()) && i < drawCount; i++) { int x = (int) Math.abs(random.nextDouble() * width); int y = (int) Math.abs(random.nextDouble() * height); x = x + minx; y = y + miny; markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y), cancel); } g.setClip(null); } break; } if (getOutline() != null) { getOutline().draw(g, affineTransform, shp, cancel); } }