X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.app.core%2Fsrc%2Forg%2Fargeo%2Fapp%2Fgeo%2FGeoUtils.java;h=1f8846d579e97518e8e37c124a1d9d8eb66f9b0b;hb=c995fe8c434321bbfedcf5aa359b4cfe721c0e91;hp=8da7c4efbd27c6e0d074aa0e90bb8e758775f3a6;hpb=07d4329086be993ff25fc6342c97b681b2e07433;p=gpl%2Fargeo-suite.git diff --git a/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java b/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java index 8da7c4e..1f8846d 100644 --- a/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java +++ b/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java @@ -1,14 +1,34 @@ package org.argeo.app.geo; +import java.io.IOException; +import java.io.Serializable; +import java.io.Writer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import javax.measure.Quantity; import javax.measure.quantity.Area; +import org.geotools.data.DefaultTransaction; +import org.geotools.data.Transaction; +import org.geotools.data.collection.ListFeatureCollection; +import org.geotools.data.shapefile.ShapefileDataStore; +import org.geotools.data.shapefile.ShapefileDataStoreFactory; +import org.geotools.data.simple.SimpleFeatureCollection; +import org.geotools.data.simple.SimpleFeatureIterator; +import org.geotools.data.simple.SimpleFeatureSource; +import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import org.geotools.referencing.crs.DefaultGeographicCRS; +import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; import org.opengis.feature.simple.SimpleFeature; +import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.geometry.MismatchedDimensionException; import org.opengis.referencing.FactoryException; import org.opengis.referencing.crs.CoordinateReferenceSystem; @@ -38,6 +58,101 @@ public class GeoUtils { } } + public static void exportToSvg(SimpleFeatureCollection features, Writer out, int width, int height) { + try { + double minY = Double.POSITIVE_INFINITY; + double maxY = Double.NEGATIVE_INFINITY; + double minX = Double.POSITIVE_INFINITY; + double maxX = Double.NEGATIVE_INFINITY; + List shapes = new ArrayList<>(); + for (SimpleFeatureIterator it = features.features(); it.hasNext();) { + SimpleFeature feature = it.next(); + StringBuffer sb = new StringBuffer(); + sb.append(" maxX) + maxX = x; + double y = -coord.y; + if (y < minY) + minY = y; + if (y > maxY) + maxY = y; + sb.append(x + "," + y + " "); + } + sb.append("\">"); + sb.append("\n"); + shapes.add(sb.toString()); + + } + double viewportHeight = maxY - minY; + double viewportWidth = maxX - minX; + out.write("\n"); + for (String shape : shapes) { + out.write(shape); + out.write("\n"); + } + out.write(""); + } catch (IOException | FactoryException | MismatchedDimensionException | TransformException e) { + throw new RuntimeException("Cannot export to SVG", e); + } + } + + /** Write a list of simple features to a shapefile. */ + public static void saveFeaturesAsShapefile(SimpleFeatureType featureType, List features, + Path shpFile) { + try { + ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); + + Map params = new HashMap<>(); + params.put("url", shpFile.toUri().toURL()); + + params.put("create spatial index", Boolean.TRUE); + + ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); + newDataStore.createSchema(featureType); + + String typeName = newDataStore.getTypeNames()[0]; + SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName); + if (featureSource instanceof SimpleFeatureStore) { + SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; + SimpleFeatureCollection collection = new ListFeatureCollection(featureType, features); + + try (Transaction transaction = new DefaultTransaction("create")) { + try { + featureStore.setTransaction(transaction); + featureStore.addFeatures(collection); + transaction.commit(); + } catch (Exception problem) { + transaction.rollback(); + throw new RuntimeException("Cannot write shapefile " + shpFile, problem); + } + } + } else { + throw new IllegalArgumentException(typeName + " does not support read/write access"); + } + } catch (IOException e) { + throw new RuntimeException("Cannot write shapefile " + shpFile, e); + } + } + /** Singleton. */ private GeoUtils() { }