I think this might depend on how you set up your output datastore, or what sort it is - I've attached my code which I've been using on Shapefiles with no issues (on master)
Ian On Tue, 19 Mar 2019 at 13:50, <paul.m...@lfv.se> wrote: > Hi Ian, > > This is how I use the generalizer: > > *while* (fit.hasNext()) { > > SimpleFeature inFeature = fit.next(); > > MultiPolygon polygon = (MultiPolygon) inFeature.getDefaultGeometry(); > > * if* (polygon!= *null* && polygon.isSimple()) { > > Coordinate[] coords = polygon.getCoordinates(); > > System.*out*.println("ant coords before = " + coords.length); > > // Genarelize all polygons with more then 5 points > > * if* (coords.length > 5) { > > DouglasPeuckerSimplifier simplifier = *new* > DouglasPeuckerSimplifier(polygon); > > simplifier.setDistanceTolerance(10); > > simplifier.setEnsureValid(*false*); > > Geometry out = simplifier.getResultGeometry(); > > coords = out.getCoordinates(); > > System.*out*.println("ant coords after = " + coords.length); > > } > > // make all coordinates (x,y,0) > > Coordinate[] coords2 = *new* Coordinate[coords.length]; > > * for*(*int* i = 0; i < coords.length; i++){ > > Coordinate c = *new* Coordinate(coords[i].x, coords[i].y, 0); > > coords2[i] = c; > > } > > // Do your thing here with the coordinates, but first make sure that > you create lines correct. > > // Just make the polygon to a line string > > // coords2 = CreateCenterLineFromCooordinates(coords2); > > LineString line = gFactory.createLineString(coords2); > > SimpleFeature feature = writer.next(); > > List<Object> l = inFeature.getAttributes(); > > * for* (*int* i = 1; i < l.size(); i++) { > > // copy all attributes but the first geometry > > feature.setAttribute(i, l.get(i)); > > } > > feature.setDefaultGeometry(line); > > writer.write(); > > } > > } > > writer.close(); > > tx.commit(); > > tx.close(); > > fit.close(); > > } > > > > I can see that the coordinates from the generalizer seems correct > (x,y,n/a) in the debugger. > > I have tried to change the coordinates, so that all has (x,y,0), even > those polygons who are not generalized. > > Then I create a line instead of a polygon. > > When I look in PostGis the objects that are generalized has no geometry = > null, but not the others. It must be something that I’m doing wrong or is > it something I miss? > > I have pointed out GeooTools 20.2 in my pom file. > > > > Kind regards, > > Paul > > > > *Från:* Ian Turton [mailto:ijtur...@gmail.com] > *Skickat:* den 15 mars 2019 18:15 > *Till:* Malm, Paul (Operations AIM) > *Kopia:* geotools-users > *Ämne:* Re: [Geotools-gt2-users] generailisation > > > > How are you getting the coordinates out? Most output formats will ignore Z > if it is not set. > > > > Ian > > > > On Fri, 15 Mar 2019 at 15:08, <paul.m...@lfv.se> wrote: > > Hi Ian! > > Thanks, it worked. But there is one problem. > > The unchanged objects has (x,y) coordinates and I’m getting (x,y,n/a) > coordinates for the changed object. > > ArcGis does not seem to be able to have this(x,y,z) coordinates or mixed > coordinate types. The changed objects > > has the correct values of x,y but does not show in ArcGis. > > Does anyone have any suggestions? > > Kind regards, > > Paul > > > > *Från:* Ian Turton [mailto:ijtur...@gmail.com] > *Skickat:* den 15 mars 2019 09:49 > *Till:* Malm, Paul (Operations AIM) > *Kopia:* geotools-users > *Ämne:* Re: [Geotools-gt2-users] generailisation > > > > You should be able to use a DouglasPeuckerSimplifier to achieve this. > Something like: > > > > DouglasPeuckerSimplifier simplifier = new DouglasPeuckerSimplifier(in); > > simplifier.setDistanceTolerance(tolerance); > > Geometry out = simplifier.getResultGeometry(); > > > > would do it. > > > > JTS also provides a TopologyPresersevingSimplifier and a > LineSegmentSimplifier that would also handle your case. > > > > Ian > > > > On Fri, 15 Mar 2019 at 06:32, <paul.m...@lfv.se> wrote: > > Hi, > > I wonder if there is a generalize function using angles. > > I have polygons almost like a rectangle/romb. 90 percent has only five > vertices, but some have more. The extra vertices is om the sides (the > straight lines). > > I would like to remove those vertices and thought that there perhaps is a > method using a max angle to find the extra vertices. > > Kind regards, > > Paul > > _______________________________________________ > GeoTools-GT2-Users mailing list > GeoTools-GT2-Users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users > > > > > -- > > Ian Turton > > > > > -- > > Ian Turton > -- Ian Turton
package com.ianturton.cookbook.operations; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import org.geotools.data.DataUtilities; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.MultiPolygon; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; import org.locationtech.jts.simplify.DouglasPeuckerSimplifier; import org.locationtech.jts.simplify.TopologyPreservingSimplifier; import org.locationtech.jts.simplify.VWSimplifier; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.feature.type.AttributeDescriptor; import org.opengis.feature.type.GeometryDescriptor; import org.opengis.geometry.MismatchedDimensionException; import org.opengis.referencing.FactoryException; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; public class Simplification { interface Generalizer { Geometry simplify(Geometry g, double tolerance); } public HashMap<String, Generalizer> generalizers = new HashMap<>(); public Simplification() { generalizers.put("DouglasPeuker", (geom, tol) -> DouglasPeuckerSimplifier.simplify(geom, tol)); generalizers.put("Topology Pereserving", (geom, tol) -> TopologyPreservingSimplifier.simplify(geom, tol)); generalizers.put("Visvalingam-Whyatt", (geom, tol) -> VWSimplifier.simplify(geom, tol)); } public static void main(String[] args) throws IOException { Simplification sim = new Simplification(); FileDataStore ds; ds = FileDataStoreFinder.getDataStore(new File(args[0])); SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.setName("simples"); SimpleFeatureType inSchema = ds.getSchema(); CoordinateReferenceSystem crs = ds.getSchema().getCoordinateReferenceSystem(); for (AttributeDescriptor a : inSchema.getAttributeDescriptors()) { if (a instanceof GeometryDescriptor) { typeBuilder.add("the_geom", a.getType().getBinding(), crs); } else { typeBuilder.add(a); } } typeBuilder.add("method", String.class); typeBuilder.setDefaultGeometry("the_geom"); SimpleFeatureType schema = typeBuilder.buildFeatureType(); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema); List<SimpleFeature> features = new ArrayList<>(); double d = 50000; // 50km GeometryFactory gf = new GeometryFactory(); try (SimpleFeatureIterator it = ds.getFeatureSource().getFeatures().features()) { while (it.hasNext()) { SimpleFeature feature = it.next(); Geometry p = (Geometry) feature.getDefaultGeometry(); builder.set("method", "base"); builder.addAll(feature.getAttributes()); builder.set("the_geom", p); SimpleFeature f = builder.buildFeature(null); features.add(f); Point c = p.getCentroid(); double x = c.getCoordinate().x; double y = c.getCoordinate().y; String code = ""; if (CRS.getAxisOrder(crs) == CRS.AxisOrder.EAST_NORTH) { code = "AUTO2:42001," + x + "," + y; } else { code = "AUTO2:42001," + y + "," + x; } CoordinateReferenceSystem auto = null; MathTransform reverse = null; Geometry r = null; try { auto = CRS.decode(code); MathTransform transform = CRS.findMathTransform(crs, auto); r = JTS.transform(p, transform); reverse = CRS.findMathTransform(auto, crs); } catch (FactoryException | MismatchedDimensionException | TransformException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (Entry<String, Generalizer> method : sim.generalizers.entrySet()) { Geometry g = null; try { Geometry simplified = method.getValue().simplify(r, d); g = JTS.transform(simplified, reverse); } catch (MismatchedDimensionException | TransformException e) { // TODO Auto-generated catch block e.printStackTrace(); } MultiPolygon s; if (g instanceof Polygon) { s = gf.createMultiPolygon(new Polygon[] { (Polygon) g }); } else { s = (MultiPolygon) g; } if (!s.isEmpty()) { builder.addAll(feature.getAttributes()); builder.set("the_geom", s); builder.set("method", method.getKey()); f = builder.buildFeature(null); if (f != null) { features.add(f); } } } } } File out = new File("generalisation.shp"); try { out.createNewFile(); FileDataStore os = FileDataStoreFinder.getDataStore(out); os.createSchema(schema); SimpleFeatureStore store = (SimpleFeatureStore) os.getFeatureSource(); store.addFeatures(DataUtilities.collection(features)); os.dispose(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
_______________________________________________ GeoTools-GT2-Users mailing list GeoTools-GT2-Users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users