Search in sources :

Example 1 with OmsNetworkAttributesBuilder

use of org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder in project hortonmachine by TheHortonMachine.

the class GeoframeInputsBuilder method process.

@Execute
public void process() throws Exception {
    checkNull(inPitfiller, inDrain, inTca, inNet, inSkyview, inBasins, outFolder);
    GridCoverage2D subBasins = getRaster(inBasins);
    CoordinateReferenceSystem crs = subBasins.getCoordinateReferenceSystem();
    List<Polygon> cells = CoverageUtilities.gridcoverageToCellPolygons(subBasins, null, true, pm);
    GridCoverage2D pit = getRaster(inPitfiller);
    GridCoverage2D sky = getRaster(inSkyview);
    GridCoverage2D drain = getRaster(inDrain);
    GridCoverage2D net = getRaster(inNet);
    GridCoverage2D tca = getRaster(inTca);
    List<Geometry> lakesList = new ArrayList<>();
    if (inLakes != null) {
        SimpleFeatureCollection lakesFC = getVector(inLakes);
        List<SimpleFeature> lakesFList = FeatureUtilities.featureCollectionToList(lakesFC);
        // remove those that don't even cover the raster envelope
        Polygon rasterBounds = FeatureUtilities.envelopeToPolygon(pit.getEnvelope2D());
        for (SimpleFeature lakeF : lakesFList) {
            Geometry lakeGeom = (Geometry) lakeF.getDefaultGeometry();
            Envelope env = lakeGeom.getEnvelopeInternal();
            if (rasterBounds.getEnvelopeInternal().intersects(env)) {
                // TODO would be good to check with basin geom
                lakesList.add(lakeGeom);
            }
        }
    }
    OmsNetworkAttributesBuilder netAttributesBuilder = new OmsNetworkAttributesBuilder();
    netAttributesBuilder.pm = pm;
    netAttributesBuilder.inDem = pit;
    netAttributesBuilder.inFlow = drain;
    netAttributesBuilder.inTca = tca;
    netAttributesBuilder.inNet = net;
    netAttributesBuilder.doHack = true;
    netAttributesBuilder.onlyDoSimpleGeoms = false;
    netAttributesBuilder.process();
    SimpleFeatureCollection outNet = netAttributesBuilder.outNet;
    // dumpVector(outNet, "/Users/hydrologis/Dropbox/hydrologis/lavori/2020_projects/15_uniTN_basins/brenta/brenta_063basins/network_attributes_full.shp");
    String userDataField = useHack ? NetworkChannel.HACKNAME : NetworkChannel.PFAFNAME;
    List<Geometry> netGeometries = FeatureUtilities.featureCollectionToGeometriesList(outNet, true, userDataField);
    Map<Integer, List<Geometry>> collected = cells.parallelStream().filter(poly -> ((Number) poly.getUserData()).doubleValue() != HMConstants.doubleNovalue).collect(Collectors.groupingBy(poly -> ((Number) poly.getUserData()).intValue()));
    SimpleFeatureBuilder basinsBuilder = getBasinsBuilder(pit.getCoordinateReferenceSystem());
    SimpleFeatureBuilder basinCentroidsBuilder = getBasinCentroidsBuilder(pit.getCoordinateReferenceSystem());
    SimpleFeatureBuilder singleNetBuilder = getSingleNetBuilder(pit.getCoordinateReferenceSystem());
    DefaultFeatureCollection allBasinsFC = new DefaultFeatureCollection();
    DefaultFeatureCollection allNetworksFC = new DefaultFeatureCollection();
    StringBuilder csvText = new StringBuilder();
    csvText.append("#id;x;y;elev_m;avgelev_m;area_km2;netlength;centroid_skyview\n");
    // aggregate basins and check for lakes
    Map<Integer, Geometry> basinId2geomMap = new HashMap<>();
    pm.beginTask("Join basin cells...", collected.size());
    int maxBasinNum = 0;
    for (Entry<Integer, List<Geometry>> entry : collected.entrySet()) {
        int basinNum = entry.getKey();
        maxBasinNum = Math.max(maxBasinNum, basinNum);
        List<Geometry> polygons = entry.getValue();
        Geometry basin = CascadedPolygonUnion.union(polygons);
        // extract largest basin
        double maxArea = Double.NEGATIVE_INFINITY;
        Geometry maxPolygon = basin;
        int numGeometries = basin.getNumGeometries();
        if (numGeometries > 1) {
            for (int i = 0; i < numGeometries; i++) {
                Geometry geometryN = basin.getGeometryN(i);
                double area = geometryN.getArea();
                if (area > maxArea) {
                    maxArea = area;
                    maxPolygon = geometryN;
                }
            }
        }
        // and can't be completely contained in a basin
        for (Geometry lakeGeom : lakesList) {
            if (maxPolygon.contains(lakeGeom)) {
                throw new ModelsIllegalargumentException("A basin can't completely contain a lake. Check your data.", this);
            }
        }
        basinId2geomMap.put(basinNum, maxPolygon);
        pm.worked(1);
    }
    pm.done();
    // if lakes are available, they have to contain at least one confluence
    for (Geometry lakeGeom : lakesList) {
        PreparedGeometry preparedLake = PreparedGeometryFactory.prepare(lakeGeom);
        boolean hasPoint = false;
        for (Geometry netGeom : netGeometries) {
            LineString line = (LineString) netGeom;
            Point startPoint = line.getStartPoint();
            Point endPoint = line.getEndPoint();
            if (preparedLake.contains(startPoint) || preparedLake.contains(endPoint)) {
                hasPoint = true;
                break;
            }
        }
        if (!hasPoint) {
            throw new ModelsIllegalargumentException("A lake has to contain at least one confluence. Check your data.", this);
        }
    }
    // if lakes are available, we need to find the basins that intersect and cut the basins on
    // them
    List<Integer> lakesIdList = new ArrayList<>();
    if (!lakesList.isEmpty()) {
        List<Basin> allBasins = new ArrayList<>();
        int nextBasinNum = maxBasinNum + 1;
        Basin rootBasin = getRootBasin(basinId2geomMap, allBasins);
        if (rootBasin != null) {
            pm.beginTask("Handle lake-basin intersections...", lakesList.size());
            for (Geometry lakeGeom : lakesList) {
                Basin outBasin = findFirstIntersecting(rootBasin, lakeGeom);
                if (outBasin != null) {
                    // found the out basin. All others intersecting will drain into this
                    List<Basin> intersectingBasins = allBasins.parallelStream().filter(tmpBasin -> {
                        boolean isNotOutBasin = tmpBasin.id != outBasin.id;
                        boolean intersectsLake = tmpBasin.basinGeometry.intersects(lakeGeom);
                        return isNotOutBasin && intersectsLake;
                    }).collect(Collectors.toList());
                    // of the intersecting find those that are completely contained
                    List<Basin> completelyContainedBasins = new ArrayList<>();
                    List<Basin> justIntersectingBasins = new ArrayList<>();
                    intersectingBasins.forEach(interBasin -> {
                        if (lakeGeom.contains(interBasin.basinGeometry)) {
                            completelyContainedBasins.add(interBasin);
                        } else {
                            justIntersectingBasins.add(interBasin);
                        }
                    });
                    intersectingBasins = justIntersectingBasins;
                    // create a new basin based on the lake
                    Basin lakeBasin = new Basin();
                    lakeBasin.basinGeometry = lakeGeom;
                    lakeBasin.id = nextBasinNum++;
                    lakesIdList.add(lakeBasin.id);
                    lakeBasin.downStreamBasin = outBasin;
                    lakeBasin.downStreamBasinId = outBasin.id;
                    lakeBasin.upStreamBasins.addAll(intersectingBasins);
                    basinId2geomMap.put(lakeBasin.id, lakeBasin.basinGeometry);
                    // the lake
                    for (Basin containedBasin : completelyContainedBasins) {
                        for (Basin tmpBasin : allBasins) {
                            if (tmpBasin.id != containedBasin.id) {
                                // them to the lake
                                if (tmpBasin.downStreamBasinId == containedBasin.id) {
                                    tmpBasin.downStreamBasinId = lakeBasin.id;
                                    tmpBasin.downStreamBasin = lakeBasin;
                                }
                                // find basins in which the completely contained drains into and
                                // relink with lake
                                Basin removeBasin = null;
                                for (Basin tmpUpBasin : tmpBasin.upStreamBasins) {
                                    if (tmpUpBasin.id == containedBasin.id) {
                                        removeBasin = tmpUpBasin;
                                    }
                                }
                                if (removeBasin != null) {
                                    tmpBasin.upStreamBasins.remove(removeBasin);
                                    tmpBasin.upStreamBasins.add(lakeBasin);
                                }
                            }
                        }
                        // at this point the containedBasin should be orphan
                        containedBasin.downStreamBasin = null;
                        containedBasin.upStreamBasins = null;
                        basinId2geomMap.remove(containedBasin.id);
                    }
                    // handle interaction with downstream basin
                    outBasin.upStreamBasins.removeAll(lakeBasin.upStreamBasins);
                    outBasin.upStreamBasins.add(lakeBasin);
                    Geometry geomToCut = basinId2geomMap.get(outBasin.id);
                    Geometry newGeom;
                    try {
                        newGeom = geomToCut.difference(lakeGeom);
                    } catch (Exception e) {
                        File folder = new File(outFolder);
                        File errorFile = new File(folder, "errors." + HMConstants.GPKG + "#error_basin_" + outBasin.id);
                        String message = "An error occurred during intersection between basin and lake geometries. IGNORING LAKE.\nGeometries written to: " + errorFile;
                        pm.errorMessage(message);
                        geomToCut.setUserData("basin_" + outBasin.id);
                        lakeGeom.setUserData("lake");
                        SimpleFeatureCollection fc = FeatureUtilities.featureCollectionFromGeometry(crs, geomToCut, lakeGeom);
                        OmsVectorWriter.writeVector(errorFile.getAbsolutePath(), fc);
                        continue;
                    }
                    outBasin.basinGeometry = newGeom;
                    basinId2geomMap.put(outBasin.id, newGeom);
                    // handle interaction with upstream basins
                    intersectingBasins.forEach(iBasin -> {
                        iBasin.downStreamBasin = lakeBasin;
                        iBasin.downStreamBasinId = lakeBasin.id;
                        Geometry basinGeomToCut = basinId2geomMap.get(iBasin.id);
                        Geometry newBasinGeom = basinGeomToCut.difference(lakeGeom);
                        basinId2geomMap.put(iBasin.id, newBasinGeom);
                    });
                    // we also need to cut the streams on the lakes
                    List<Geometry> cutNetGeometries = new ArrayList<>();
                    for (Geometry netGeom : netGeometries) {
                        if (netGeom.intersects(lakeGeom)) {
                            Geometry difference = netGeom.difference(lakeGeom);
                            if (!difference.isEmpty()) {
                                if (difference.getNumGeometries() > 1) {
                                    // choose longest
                                    Geometry longest = null;
                                    double maxLength = -1;
                                    for (int i = 0; i < difference.getNumGeometries(); i++) {
                                        Geometry geometryN = difference.getGeometryN(i);
                                        double l = geometryN.getLength();
                                        if (l > maxLength) {
                                            maxLength = l;
                                            longest = geometryN;
                                        }
                                    }
                                    difference = longest;
                                }
                                cutNetGeometries.add(difference);
                                difference.setUserData(netGeom.getUserData());
                            }
                        } else {
                            cutNetGeometries.add(netGeom);
                        }
                    }
                    netGeometries = cutNetGeometries;
                }
            }
            // finally rewrite the topology file.
            File topoFile = new File(inGeoframeTopology);
            String topoFilename = FileUtilities.getNameWithoutExtention(topoFile);
            File newTopoFile = new File(topoFile.getParentFile(), topoFilename + "_lakes.txt");
            List<String> topology = new ArrayList<>();
            writeTopology(topology, rootBasin);
            StringBuilder sb = new StringBuilder();
            topology.forEach(record -> sb.append(record).append("\n"));
            FileUtilities.writeFile(sb.toString(), newTopoFile);
        } else {
            pm.errorMessage("Unable to find the basin topology.");
        }
    }
    pm.beginTask("Extract vector basins...", basinId2geomMap.size());
    for (Entry<Integer, Geometry> entry : basinId2geomMap.entrySet()) {
        int basinNum = entry.getKey();
        pm.message("Processing basin " + basinNum + "...");
        Geometry basinPolygon = entry.getValue();
        // for lakes ==0
        double mainNetLength = 0;
        List<LineString> netPieces = new ArrayList<>();
        List<Integer> checkValueList = new ArrayList<>();
        boolean isLake = lakesIdList.contains(basinNum);
        if (!isLake) {
            // get network pieces inside basin
            int minCheckValue = Integer.MAX_VALUE;
            HashMap<Integer, List<LineString>> checkValueList4Lines = new HashMap<>();
            for (Geometry netGeom : netGeometries) {
                if (netGeom.intersects(basinPolygon)) {
                    LengthIndexedLine lil = new LengthIndexedLine(netGeom);
                    Coordinate centerCoord = lil.extractPoint(0.5);
                    double value = CoverageUtilities.getValue(subBasins, centerCoord);
                    if ((int) value == basinNum) {
                        Object userData = netGeom.getUserData();
                        int checkValue;
                        if (useHack) {
                            checkValue = Integer.parseInt(userData.toString());
                        } else {
                            String pfaf = userData.toString();
                            PfafstetterNumber p = new PfafstetterNumber(pfaf);
                            checkValue = p.getOrder();
                        }
                        minCheckValue = Math.min(minCheckValue, checkValue);
                        checkValueList.add(checkValue);
                        List<LineString> list = checkValueList4Lines.get(checkValue);
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        list.add((LineString) netGeom);
                        checkValueList4Lines.put(checkValue, list);
                        netPieces.add((LineString) netGeom);
                    }
                }
            }
            if (minCheckValue != Integer.MAX_VALUE) {
                List<LineString> minCheckValueLines = checkValueList4Lines.get(minCheckValue);
                for (LineString minCheckValueLine : minCheckValueLines) {
                    mainNetLength += minCheckValueLine.getLength();
                }
            }
        }
        Envelope basinEnvelope = basinPolygon.getEnvelopeInternal();
        Point basinCentroid = basinPolygon.getCentroid();
        double areaM2 = basinPolygon.getArea();
        double areaKm2 = areaM2 / 1000000.0;
        Coordinate point = basinCentroid.getCoordinate();
        double elev = CoverageUtilities.getValue(pit, point);
        double skyview = CoverageUtilities.getValue(sky, point);
        // Extracting raster data for each basin
        ReferencedEnvelope basinRefEnvelope = new ReferencedEnvelope(basinEnvelope, crs);
        GridCoverage2D clipped = CoverageUtilities.clipCoverage(subBasins, basinRefEnvelope);
        WritableRaster clippedWR = CoverageUtilities.renderedImage2IntWritableRaster(clipped.getRenderedImage(), false);
        // we need to consider the lakes and lake cuts, so the polygon needs to be used
        PreparedGeometry preparedBasinPolygon = PreparedGeometryFactory.prepare(basinPolygon);
        RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(clipped);
        GridGeometry2D clippedGG = clipped.getGridGeometry();
        int cols = regionMap.getCols();
        int rows = regionMap.getRows();
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                Coordinate coord = CoverageUtilities.coordinateFromColRow(c, r, clippedGG);
                if (preparedBasinPolygon.intersects(GeometryUtilities.gf().createPoint(coord))) {
                    clippedWR.setSample(c, r, 0, basinNum);
                // TODO check
                // int value = clippedWR.getSample(c, r, 0);
                // if (value != basinNum) {
                // clippedWR.setSample(c, r, 0, HMConstants.intNovalue);
                // }
                } else {
                    clippedWR.setSample(c, r, 0, HMConstants.intNovalue);
                }
            }
        }
        File basinFolder = makeBasinFolder(basinNum);
        GridCoverage2D maskCoverage = CoverageUtilities.buildCoverage("basin" + basinNum, clippedWR, regionMap, crs);
        GridCoverage2D clippedPit = CoverageUtilities.clipCoverage(pit, basinRefEnvelope);
        GridCoverage2D cutPit = CoverageUtilities.coverageValuesMapper(clippedPit, maskCoverage);
        File pitFile = new File(basinFolder, "dtm_" + basinNum + ".asc");
        if (!pitFile.exists() || doOverWrite) {
            dumpRaster(cutPit, pitFile.getAbsolutePath());
        }
        double[] minMaxAvgSum = OmsRasterSummary.getMinMaxAvgSum(cutPit);
        double avgElev = minMaxAvgSum[2];
        GridCoverage2D clippedSky = CoverageUtilities.clipCoverage(sky, basinRefEnvelope);
        GridCoverage2D cutSky = CoverageUtilities.coverageValuesMapper(clippedSky, maskCoverage);
        File skyFile = new File(basinFolder, "sky_" + basinNum + ".asc");
        if (!skyFile.exists() || doOverWrite) {
            dumpRaster(cutSky, skyFile.getAbsolutePath());
        }
        GridCoverage2D clippedDrain = CoverageUtilities.clipCoverage(drain, basinRefEnvelope);
        GridCoverage2D cutDrain = CoverageUtilities.coverageValuesMapper(clippedDrain, maskCoverage);
        File drainFile = new File(basinFolder, "drain_" + basinNum + ".asc");
        if (!drainFile.exists() || doOverWrite) {
            dumpRaster(cutDrain, drainFile.getAbsolutePath());
        }
        GridCoverage2D clippedNet = CoverageUtilities.clipCoverage(net, basinRefEnvelope);
        GridCoverage2D cutNet = CoverageUtilities.coverageValuesMapper(clippedNet, maskCoverage);
        File netFile = new File(basinFolder, "net_" + basinNum + ".asc");
        if (!netFile.exists() || doOverWrite) {
            dumpRaster(cutNet, netFile.getAbsolutePath());
        }
        // OmsRescaledDistance rescaledDistance1 = new OmsRescaledDistance();
        // rescaledDistance1.pm = pm;
        // rescaledDistance1.inElev = cutPit;
        // rescaledDistance1.inFlow = cutDrain;
        // rescaledDistance1.inNet = cutNet;
        // rescaledDistance1.pRatio = 1;
        // rescaledDistance1.process();
        // File rescaled1File = new File(basinFolder, "rescaleddistance_1_" + basinNum + ".asc");
        // if (!rescaled1File.exists() || doOverWrite) {
        // dumpRaster(rescaledDistance1.outRescaled, rescaled1File.getAbsolutePath());
        // }
        // 
        // OmsRescaledDistance rescaledDistance = new OmsRescaledDistance();
        // rescaledDistance.pm = pm;
        // rescaledDistance.inElev = cutPit;
        // rescaledDistance.inFlow = cutDrain;
        // rescaledDistance.inNet = cutNet;
        // rescaledDistance.pRatio = pRatio;
        // rescaledDistance.process();
        // File rescaledRatioFile = new File(basinFolder, "rescaleddistance_" + pRatio + "_" + basinNum + ".asc");
        // if (!rescaledRatioFile.exists() || doOverWrite) {
        // dumpRaster(rescaledDistance.outRescaled, rescaledRatioFile.getAbsolutePath());
        // }
        // finalize feature writing
        // BASINS
        Geometry dumpBasin = basinPolygon;
        if (basinPolygon instanceof Polygon) {
            dumpBasin = GeometryUtilities.gf().createMultiPolygon(new Polygon[] { (Polygon) basinPolygon });
        }
        Object[] basinValues = new Object[] { dumpBasin, basinNum, point.x, point.y, elev, avgElev, areaKm2, mainNetLength, skyview, isLake ? 1 : 0 };
        basinsBuilder.addAll(basinValues);
        SimpleFeature basinFeature = basinsBuilder.buildFeature(null);
        allBasinsFC.add(basinFeature);
        // dump single subbasin
        DefaultFeatureCollection singleBasin = new DefaultFeatureCollection();
        singleBasin.add(basinFeature);
        File basinShpFile = new File(basinFolder, "subbasins_complete_ID_" + basinNum + ".shp");
        if (!basinShpFile.exists() || doOverWrite) {
            dumpVector(singleBasin, basinShpFile.getAbsolutePath());
        }
        Object[] centroidValues = new Object[] { basinCentroid, basinNum, point.x, point.y, elev, avgElev, areaKm2, mainNetLength, skyview };
        basinCentroidsBuilder.addAll(centroidValues);
        SimpleFeature basinCentroidFeature = basinCentroidsBuilder.buildFeature(null);
        // dump single centroid
        DefaultFeatureCollection singleCentroid = new DefaultFeatureCollection();
        singleCentroid.add(basinCentroidFeature);
        File centroidShpFile = new File(basinFolder, "centroid_ID_" + basinNum + ".shp");
        if (!centroidShpFile.exists() || doOverWrite) {
            dumpVector(singleCentroid, centroidShpFile.getAbsolutePath());
        }
        // CHANNELS
        if (!netPieces.isEmpty()) {
            DefaultFeatureCollection singleNet = new DefaultFeatureCollection();
            for (int i = 0; i < netPieces.size(); i++) {
                LineString netLine = netPieces.get(i);
                Integer checkValue = checkValueList.get(i);
                Object[] netValues = new Object[] { netLine, basinNum, netLine.getLength(), checkValue };
                singleNetBuilder.addAll(netValues);
                SimpleFeature singleNetFeature = singleNetBuilder.buildFeature(null);
                allNetworksFC.add(singleNetFeature);
                singleNet.add(singleNetFeature);
            }
            File netShpFile = new File(basinFolder, "network_complete_ID_" + basinNum + ".shp");
            if (!netShpFile.exists() || doOverWrite) {
                dumpVector(singleNet, netShpFile.getAbsolutePath());
            }
        }
        csvText.append(basinNum).append(";");
        csvText.append(point.x).append(";");
        csvText.append(point.y).append(";");
        csvText.append(elev).append(";");
        csvText.append(avgElev).append(";");
        csvText.append(areaKm2).append(";");
        csvText.append(mainNetLength).append(";");
        csvText.append(skyview).append("\n");
        pm.worked(1);
    }
    pm.done();
    File folder = new File(outFolder);
    File basinShpFile = new File(folder, "subbasins_complete.shp");
    if (!basinShpFile.exists() || doOverWrite) {
        dumpVector(allBasinsFC, basinShpFile.getAbsolutePath());
    }
    File netShpFile = new File(folder, "network_complete.shp");
    if (!netShpFile.exists() || doOverWrite) {
        dumpVector(allNetworksFC, netShpFile.getAbsolutePath());
    }
    File csvFile = new File(folder, "subbasins.csv");
    if (!csvFile.exists() || doOverWrite) {
        FileUtilities.writeFile(csvText.toString(), csvFile);
    }
}
Also used : HMModel(org.hortonmachine.gears.libs.modules.HMModel) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Label(oms3.annotations.Label) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) Coordinate(org.locationtech.jts.geom.Coordinate) Status(oms3.annotations.Status) Execute(oms3.annotations.Execute) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Map(java.util.Map) HMConstants(org.hortonmachine.gears.libs.modules.HMConstants) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) OmsNetworkAttributesBuilder(org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder) OmsRescaledDistance(org.hortonmachine.hmachine.modules.basin.rescaleddistance.OmsRescaledDistance) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) Point(org.locationtech.jts.geom.Point) Collectors(java.util.stream.Collectors) CoverageUtilities(org.hortonmachine.gears.utils.coverage.CoverageUtilities) List(java.util.List) OmsVectorWriter(org.hortonmachine.gears.io.vectorwriter.OmsVectorWriter) Entry(java.util.Map.Entry) Polygon(org.locationtech.jts.geom.Polygon) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) Geometry(org.locationtech.jts.geom.Geometry) PfafstetterNumber(org.hortonmachine.hmachine.modules.network.PfafstetterNumber) Keywords(oms3.annotations.Keywords) IHMProgressMonitor(org.hortonmachine.gears.libs.monitor.IHMProgressMonitor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) PreparedGeometryFactory(org.locationtech.jts.geom.prep.PreparedGeometryFactory) CascadedPolygonUnion(org.locationtech.jts.operation.union.CascadedPolygonUnion) NetworkChannel(org.hortonmachine.hmachine.modules.network.networkattributes.NetworkChannel) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Author(oms3.annotations.Author) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) In(oms3.annotations.In) HashMap(java.util.HashMap) License(oms3.annotations.License) TreeSet(java.util.TreeSet) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ArrayList(java.util.ArrayList) Description(oms3.annotations.Description) Name(oms3.annotations.Name) GeometryUtilities(org.hortonmachine.gears.utils.geometry.GeometryUtilities) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) Iterator(java.util.Iterator) FileUtilities(org.hortonmachine.gears.utils.files.FileUtilities) IOException(java.io.IOException) FeatureUtilities(org.hortonmachine.gears.utils.features.FeatureUtilities) OmsRasterSummary(org.hortonmachine.gears.modules.r.summary.OmsRasterSummary) File(java.io.File) LineString(org.locationtech.jts.geom.LineString) LengthIndexedLine(org.locationtech.jts.linearref.LengthIndexedLine) RegionMap(org.hortonmachine.gears.utils.RegionMap) UI(oms3.annotations.UI) WritableRaster(java.awt.image.WritableRaster) Envelope(org.locationtech.jts.geom.Envelope) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LineString(org.locationtech.jts.geom.LineString) OmsNetworkAttributesBuilder(org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) List(java.util.List) ArrayList(java.util.ArrayList) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) PfafstetterNumber(org.hortonmachine.hmachine.modules.network.PfafstetterNumber) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) File(java.io.File) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) PfafstetterNumber(org.hortonmachine.hmachine.modules.network.PfafstetterNumber) LengthIndexedLine(org.locationtech.jts.linearref.LengthIndexedLine) WritableRaster(java.awt.image.WritableRaster) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) Point(org.locationtech.jts.geom.Point) Point(org.locationtech.jts.geom.Point) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) IOException(java.io.IOException) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) LineString(org.locationtech.jts.geom.LineString) Execute(oms3.annotations.Execute)

Example 2 with OmsNetworkAttributesBuilder

use of org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder in project hortonmachine by TheHortonMachine.

the class NetworkAttributesBuilder method process.

@Execute
public void process() throws Exception {
    OmsNetworkAttributesBuilder networkattributesbuilder = new OmsNetworkAttributesBuilder();
    networkattributesbuilder.inNet = getRaster(inNet);
    networkattributesbuilder.inFlow = getRaster(inFlow);
    networkattributesbuilder.inTca = getRaster(inTca);
    networkattributesbuilder.doHack = doHack;
    networkattributesbuilder.pm = pm;
    networkattributesbuilder.doProcess = doProcess;
    networkattributesbuilder.doReset = doReset;
    networkattributesbuilder.process();
    dumpVector(networkattributesbuilder.outNet, outNet);
    dumpRaster(networkattributesbuilder.outHack, outHack);
}
Also used : OmsNetworkAttributesBuilder(org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder) Execute(oms3.annotations.Execute)

Example 3 with OmsNetworkAttributesBuilder

use of org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder in project hortonmachine by TheHortonMachine.

the class TestExtractNetwork method testOmsNetworkAttributesBuilder.

public void testOmsNetworkAttributesBuilder() throws Exception {
    HashMap<String, Double> envelopeParams = HMTestMaps.getEnvelopeparams();
    CoordinateReferenceSystem crs = HMTestMaps.getCrs();
    double[][] flowData = HMTestMaps.flowData;
    GridCoverage2D flowCoverage = CoverageUtilities.buildCoverage("flow", flowData, envelopeParams, crs, true);
    double[][] tcaData = HMTestMaps.tcaData;
    GridCoverage2D tcaCoverage = CoverageUtilities.buildCoverage("tca", tcaData, envelopeParams, crs, true);
    double[][] netData = HMTestMaps.extractNet0Data;
    GridCoverage2D netCoverage = CoverageUtilities.buildCoverage("net", netData, envelopeParams, crs, true);
    OmsNetworkAttributesBuilder extractNetwork = new OmsNetworkAttributesBuilder();
    extractNetwork.pm = pm;
    extractNetwork.inFlow = flowCoverage;
    extractNetwork.inTca = tcaCoverage;
    extractNetwork.inNet = netCoverage;
    extractNetwork.process();
    SimpleFeatureCollection networkFC = extractNetwork.outNet;
    List<FeatureMate> matesList = FeatureUtilities.featureCollectionToMatesList(networkFC);
    for (FeatureMate featureMate : matesList) {
        if (featureMate.getAttribute(NetworkChannel.PFAFNAME, String.class).equals("1")) {
            assertEquals(1, featureMate.getAttribute(NetworkChannel.HACKNAME, Integer.class).intValue());
            assertEquals(2, featureMate.getAttribute(NetworkChannel.STRAHLERNAME, Integer.class).intValue());
            assertEquals("LINESTRING (1640845 5139885, 1640815 5139885, 1640785 5139885, 1640755 5139885, 1640725 5139885, 1640695 5139915)", featureMate.getGeometry().toText());
        } else if (featureMate.getAttribute(NetworkChannel.PFAFNAME, String.class).equals("3")) {
            assertEquals(1, featureMate.getAttribute(NetworkChannel.HACKNAME, Integer.class).intValue());
            assertEquals(1, featureMate.getAttribute(NetworkChannel.STRAHLERNAME, Integer.class).intValue());
            assertEquals("LINESTRING (1640875 5139885, 1640845 5139885)", featureMate.getGeometry().toText());
        } else if (featureMate.getAttribute(NetworkChannel.PFAFNAME, String.class).equals("2.1")) {
            assertEquals(2, featureMate.getAttribute(NetworkChannel.HACKNAME, Integer.class).intValue());
            assertEquals(1, featureMate.getAttribute(NetworkChannel.STRAHLERNAME, Integer.class).intValue());
            assertEquals("LINESTRING (1640875 5139915, 1640845 5139885)", featureMate.getGeometry().toText());
        } else {
            throw new RuntimeException();
        }
    }
}
Also used : OmsNetworkAttributesBuilder(org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) FeatureMate(org.hortonmachine.gears.utils.features.FeatureMate) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection)

Example 4 with OmsNetworkAttributesBuilder

use of org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder in project hortonmachine by TheHortonMachine.

the class LW02_NetworkAttributesBuilder method process.

@Execute
public void process() throws Exception {
    OmsNetworkAttributesBuilder networkattributesbuilder = new OmsNetworkAttributesBuilder();
    networkattributesbuilder.inNet = getRaster(inNet);
    networkattributesbuilder.inFlow = getRaster(inFlow);
    networkattributesbuilder.inTca = getRaster(inTca);
    networkattributesbuilder.doHack = false;
    networkattributesbuilder.pm = pm;
    networkattributesbuilder.doProcess = doProcess;
    networkattributesbuilder.doReset = doReset;
    networkattributesbuilder.process();
    dumpRaster(networkattributesbuilder.outHack, null);
    dumpVector(networkattributesbuilder.outNet, outNet);
}
Also used : OmsNetworkAttributesBuilder(org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder) Execute(oms3.annotations.Execute)

Aggregations

OmsNetworkAttributesBuilder (org.hortonmachine.hmachine.modules.network.networkattributes.OmsNetworkAttributesBuilder)4 Execute (oms3.annotations.Execute)3 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)2 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)2 WritableRaster (java.awt.image.WritableRaster)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 Author (oms3.annotations.Author)1 Description (oms3.annotations.Description)1 In (oms3.annotations.In)1 Keywords (oms3.annotations.Keywords)1 Label (oms3.annotations.Label)1