Search in sources :

Example 6 with GeometryAttribute

use of org.opengis.feature.GeometryAttribute in project mkgmap by openstreetmap.

the class PrecompSeaGenerator method runSeaGeneration.

public void runSeaGeneration() throws MismatchedDimensionException, TransformException, IOException, InterruptedException {
    createShapefileAccess();
    // get all tiles that need to be processed
    List<uk.me.parabola.imgfmt.app.Area> remainingTiles = getTiles();
    // initialize the count down so that it is possible to get the
    // information when all tiles are finished
    CountDownLatch tilesCountdown = new CountDownLatch(remainingTiles.size());
    // start a printer that outputs how many tiles still need to be
    // processed
    new ProgressPrinter(tilesCountdown).start();
    // start the saver thread that stores the tiles to disc and creates
    // the index file
    PrecompSeaSaver precompSaver = new PrecompSeaSaver(outputDir, usePbfFormat);
    new Thread(precompSaver, "SaveThread").start();
    // requirements
    while (remainingTiles.isEmpty() == false) {
        // create a list with all tiles that are processed within this cycle
        List<uk.me.parabola.imgfmt.app.Area> tiles = new ArrayList<uk.me.parabola.imgfmt.app.Area>();
        tiles.addAll(remainingTiles.subList(0, Math.min(tilesPerCycle, remainingTiles.size())));
        remainingTiles.subList(0, Math.min(tilesPerCycle, remainingTiles.size())).clear();
        // create the mergers that merge the data of one tile
        List<PrecompSeaMerger> mergers = createMergers(tiles, tilesCountdown, precompSaver.getQueue());
        // create an overall area for a simple check if a polygon read from the
        // shape file intersects one of the currently processed sea tiles
        Area tileArea = new Area();
        for (PrecompSeaMerger m : mergers) {
            tileArea.add(new Area(m.getTileBounds()));
            // start the mergers
            service.execute(m);
        }
        openShapefile();
        int numPolygon = 0;
        long lastInfo = System.currentTimeMillis();
        // read all polygons from the shape file and add them to the queues of the
        // merger threads
        Geometry wgs84Poly = null;
        while (shapeIterator.hasNext()) {
            Feature feature = shapeIterator.next();
            GeometryAttribute geom = feature.getDefaultGeometryProperty();
            Geometry poly = (Geometry) geom.getValue();
            if (poly == null) {
                continue;
            }
            try {
                wgs84Poly = transformToWGS84(poly);
            } catch (Exception exp) {
                System.err.println(exp);
                continue;
            }
            if (wgs84Poly.getNumGeometries() != 1) {
                // only simple polygons are supported by now
                // maybe this could be changed in future?
                System.err.println("Polygon from shapefile has " + wgs84Poly.getNumGeometries() + " geometries. Only one geometry is supported.");
                System.err.println("Skip polygon.");
                continue;
            }
            Geometry bounds = wgs84Poly.getEnvelope();
            if (bounds.isEmpty()) {
                System.err.println("Empty or non polygon: " + bounds);
            } else {
                Area polyBounds = convertToArea(bounds);
                // currently processed
                if (polyBounds.intersects(tileArea.getBounds2D())) {
                    // yes it touches at least one tile => convert it to
                    // a java.awt.geom.Area object
                    Area polyAsArea = convertToArea(wgs84Poly.getGeometryN(0));
                    // polygon to the queues of them
                    for (PrecompSeaMerger mThread : mergers) {
                        if (mThread.getTileBounds().intersects(polyAsArea.getBounds2D())) {
                            try {
                                mThread.getQueue().put(polyAsArea);
                            } catch (InterruptedException exp) {
                                exp.printStackTrace();
                            }
                        }
                    }
                }
                numPolygon++;
                if ((numPolygon) % 50000 == 0 || System.currentTimeMillis() - lastInfo > 30000) {
                    // print out the current number of polygons already processed
                    System.out.println("Worked out " + (numPolygon) + " polygons");
                    lastInfo = System.currentTimeMillis();
                }
            }
        }
        closeShapefile();
        System.out.println("Reading shapefile finished");
        // signal all mergers that all polygons have been read
        for (PrecompSeaMerger mThread : mergers) {
            mThread.signalInputComplete();
        }
        // may occurr
        while (tilesCountdown.getCount() > remainingTiles.size() + 2 * tilesPerCycle) {
            Thread.sleep(50L);
        }
    }
    // wait until all tiles have been merged
    tilesCountdown.await();
    // wait until the saver for the tiles is finished
    precompSaver.waitForFinish();
    // shutdown the executor service
    service.shutdown();
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Feature(org.opengis.feature.Feature) FactoryException(org.opengis.referencing.FactoryException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) CQLException(org.geotools.filter.text.cql2.CQLException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Geometry(com.vividsolutions.jts.geom.Geometry) Area(java.awt.geom.Area) GeometryAttribute(org.opengis.feature.GeometryAttribute)

Example 7 with GeometryAttribute

use of org.opengis.feature.GeometryAttribute in project polymap4-core by Polymap4.

the class RFeatureStore method addFeatures.

// FeatureStore ***************************************
@Override
public List addFeatures(FeatureCollection features) throws IOException {
    final List<FeatureId> fids = new ArrayList();
    try {
        startModification();
        List<Exception> exc = new ArrayList();
        features.accepts(new FeatureVisitor() {

            public void visit(Feature feature) {
                // assert feature instanceof RFeature : "Added features must be RFeatures. See RFeatureStore#newFeature().";
                try {
                    // RFeature
                    if (feature instanceof RFeature) {
                        txState.updater().store(((RFeature) feature).state);
                        fids.add(feature.getIdentifier());
                    } else // SimpleFeature -> convert
                    if (feature instanceof SimpleFeature) {
                        RFeature newFeature = newFeature(feature.getIdentifier() != null ? feature.getIdentifier().getID() : null);
                        for (Property prop : feature.getProperties()) {
                            newFeature.getProperty(prop.getName()).setValue(prop.getValue());
                        }
                        // sanity check: geom
                        GeometryAttribute geom = feature.getDefaultGeometryProperty();
                        if (geom != null && geom.getValue() == null) {
                            throw new RuntimeException("Feature has no geometry: " + feature.getIdentifier().getID());
                        }
                        txState.updater().store(newFeature.state);
                        fids.add(newFeature.getIdentifier());
                    } else {
                        throw new UnsupportedOperationException("Added features must be instance of RFeature or SimpleFeature");
                    }
                } catch (Exception e) {
                    exc.add(e);
                }
            }
        }, null);
        if (!exc.isEmpty()) {
            throw exc.get(0);
        }
        completeModification(true);
    } catch (IOException e) {
        completeModification(false);
        throw e;
    } catch (Throwable e) {
        completeModification(false);
        throw new RuntimeException(e);
    }
    return fids;
}
Also used : ArrayList(java.util.ArrayList) FeatureVisitor(org.opengis.feature.FeatureVisitor) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FeatureId(org.opengis.filter.identity.FeatureId) GeometryAttribute(org.opengis.feature.GeometryAttribute) Property(org.opengis.feature.Property)

Aggregations

GeometryAttribute (org.opengis.feature.GeometryAttribute)7 Feature (org.opengis.feature.Feature)4 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)3 Property (org.opengis.feature.Property)3 Function (com.google.common.base.Function)2 Optional (com.google.common.base.Optional)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Nullable (javax.annotation.Nullable)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 Point (com.vividsolutions.jts.geom.Point)1 Polygon (com.vividsolutions.jts.geom.Polygon)1 Area (java.awt.geom.Area)1 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 GeometryAttributeImpl (org.geotools.feature.GeometryAttributeImpl)1 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)1