Search in sources :

Example 1 with Out

use of oms3.annotations.Out in project hortonmachine by TheHortonMachine.

the class OmsOnlineTilesDownloader method process.

@Execute
public void process() throws Exception {
    checkNull(inPath, inServiceUrl, pEpsg, pMinzoom, pMaxzoom, pWest, pEast, pSouth, pNorth);
    CoordinateReferenceSystem boundsCrs = CrsUtilities.getCrsFromEpsg(pEpsg, null);
    CoordinateReferenceSystem mercatorCrs = CrsUtilities.getCrsFromEpsg(EPSG_MERCATOR, null);
    CoordinateReferenceSystem latLongCrs = CrsUtilities.getCrsFromEpsg(EPSG_LATLONG, null);
    ReferencedEnvelope inBounds = new ReferencedEnvelope(pWest, pEast, pSouth, pNorth, boundsCrs);
    MathTransform in2MercatorTransform = CRS.findMathTransform(boundsCrs, mercatorCrs);
    Envelope mercatorEnvelope = JTS.transform(inBounds, in2MercatorTransform);
    ReferencedEnvelope mercatorBounds = new ReferencedEnvelope(mercatorEnvelope, mercatorCrs);
    MathTransform transform = CRS.findMathTransform(boundsCrs, latLongCrs);
    Envelope latLongBounds = JTS.transform(inBounds, transform);
    Coordinate latLongCentre = latLongBounds.centre();
    File inFolder = new File(inPath);
    File baseFolder = new File(inFolder, pName);
    double w = latLongBounds.getMinX();
    double s = latLongBounds.getMinY();
    double e = latLongBounds.getMaxX();
    double n = latLongBounds.getMaxY();
    GlobalMercator mercator = new GlobalMercator();
    for (int z = pMinzoom; z <= pMaxzoom; z++) {
        // get ul and lr tile number in GOOGLE tiles
        int[] llTileXY = mercator.GoogleTile(s, w, z);
        int[] urTileXY = mercator.GoogleTile(n, e, z);
        int startXTile = Math.min(llTileXY[0], urTileXY[0]);
        int endXTile = Math.max(llTileXY[0], urTileXY[0]);
        int startYTile = Math.min(llTileXY[1], urTileXY[1]);
        int endYTile = Math.max(llTileXY[1], urTileXY[1]);
        int tileNum = 0;
        ReferencedEnvelope levelBounds = new ReferencedEnvelope();
        pm.beginTask("Generating tiles at zoom level: " + z, (endXTile - startXTile + 1));
        for (int i = startXTile; i <= endXTile; i++) {
            for (int j = startYTile; j <= endYTile; j++) {
                tileNum++;
                Envelope bounds = mercator.TileLatLonBounds(i, j, z);
                ReferencedEnvelope tmpBounds = new ReferencedEnvelope(bounds, latLongCrs);
                levelBounds.expandToInclude(tmpBounds);
                if (!doDryrun) {
                    int[] onlineTileNumbers = { i, j };
                    int[] fileNameTileNumbers = { i, j };
                    // switch( pType ) {
                    // case 1:
                    // need to convert in TMS format
                    int[] tmsNUms = mercator.TMSTileFromGoogleTile(i, j, z);
                    fileNameTileNumbers = tmsNUms;
                    // break;
                    // case 0:
                    // default:
                    // break;
                    // }
                    File imageFolder = new File(baseFolder, z + "/" + fileNameTileNumbers[0]);
                    if (!imageFolder.exists()) {
                        if (!imageFolder.mkdirs()) {
                            throw new ModelsIOException("Unable to create folder:" + imageFolder, this);
                        }
                    }
                    File imageFile = new File(imageFolder, fileNameTileNumbers[1] + ".png");
                    if (imageFile.exists()) {
                        continue;
                    }
                    String tmp = inServiceUrl.replaceFirst("ZZZ", String.valueOf(z));
                    tmp = tmp.replaceFirst("XXX", String.valueOf(onlineTileNumbers[0]));
                    tmp = tmp.replaceFirst("YYY", String.valueOf(onlineTileNumbers[1]));
                    // System.out.println(tmp);
                    URL url = new URL(tmp);
                    InputStream imgStream = null;
                    OutputStream out = null;
                    try {
                        imgStream = url.openStream();
                        out = new FileOutputStream(imageFile);
                        int read = 0;
                        byte[] bytes = new byte[1024];
                        while ((read = imgStream.read(bytes)) != -1) {
                            out.write(bytes, 0, read);
                        }
                    } catch (Exception ex) {
                        pm.errorMessage("Unable to get image: " + tmp);
                    } finally {
                        if (imgStream != null)
                            imgStream.close();
                        if (out != null) {
                            out.flush();
                            out.close();
                        }
                    }
                }
            }
            pm.worked(1);
        }
        pm.done();
        pm.message("Zoom level: " + z + " has " + tileNum + " tiles.");
        pm.message("Boundary covered at Zoom level: " + z + ": " + levelBounds);
        pm.message("Total boundary wanted: " + mercatorBounds);
    }
    StringBuilder properties = new StringBuilder();
    properties.append("url=").append(pName).append("/ZZZ/XXX/YYY.png\n");
    properties.append("minzoom=").append(pMinzoom).append("\n");
    properties.append("maxzoom=").append(pMaxzoom).append("\n");
    properties.append("center=").append(latLongCentre.x).append(" ").append(latLongCentre.y).append("\n");
    properties.append("type=tms").append("\n");
    File propFile = new File(inFolder, pName + ".mapurl");
    FileUtilities.writeFile(properties.toString(), propFile);
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) URL(java.net.URL) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Coordinate(org.locationtech.jts.geom.Coordinate) FileOutputStream(java.io.FileOutputStream) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) File(java.io.File) Execute(oms3.annotations.Execute)

Example 2 with Out

use of oms3.annotations.Out in project hortonmachine by TheHortonMachine.

the class ModelsSupporter method generateTemplate.

public static String generateTemplate(Object parent) {
    Class<?> class1 = parent.getClass();
    String name = class1.getSimpleName();
    StringBuilder sb = new StringBuilder();
    String newName = name;
    String varName = name.toLowerCase();
    sb.append("def ").append(varName).append(" = new ");
    sb.append(newName).append("()\n");
    java.lang.reflect.Field[] fields = class1.getFields();
    for (java.lang.reflect.Field field : fields) {
        String fname = field.getName();
        if (fname.equals("pm") || fname.equals("gf") || fname.equals("doProcess") || fname.equals("doReset")) {
            continue;
        }
        Class<?> cl = field.getType();
        Out out = field.getAnnotation(Out.class);
        if (out != null) {
            continue;
        }
        if (cl.isAssignableFrom(String.class)) {
            sb.append(varName).append(".").append(fname).append(" = \"\"\n");
        } else {
            sb.append(varName).append(".").append(fname).append(" = ?\n");
        }
    }
    sb.append(varName).append(".process()\n");
    // }
    return sb.toString();
}
Also used : Field(java.lang.reflect.Field) Field(java.lang.reflect.Field) Out(oms3.annotations.Out)

Example 3 with Out

use of oms3.annotations.Out 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 4 with Out

use of oms3.annotations.Out in project hortonmachine by TheHortonMachine.

the class ComponentAccess method rangeCheck.

public static void rangeCheck(Object comp, boolean in, boolean out) throws Exception {
    ComponentAccess cp = new ComponentAccess(comp);
    Collection<Access> acc = new ArrayList<Access>();
    if (in) {
        acc.addAll(cp.inputs());
    }
    if (out) {
        acc.addAll(cp.outputs());
    }
    for (Access a : acc) {
        String name = a.getField().getName();
        Object val = a.getFieldValue();
        Range range = a.getField().getAnnotation(Range.class);
        if (range != null) {
            if (val instanceof Number) {
                double v = ((Number) val).doubleValue();
                if (!Annotations.inRange(range, v)) {
                    throw new ComponentException(name + " not in range " + v);
                }
            } else if (val instanceof double[]) {
                double[] v = (double[]) val;
                for (int i = 0; i < v.length; i++) {
                    if (!Annotations.inRange(range, v[i])) {
                        throw new ComponentException(name + " not in range " + v[i]);
                    }
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Range(oms3.annotations.Range)

Example 5 with Out

use of oms3.annotations.Out in project hortonmachine by TheHortonMachine.

the class FieldObjectAccess method out.

/**
 * a field is sending a new value (out)
 *
 * @throws java.lang.Exception
 */
@Override
public void out() throws Exception {
    Object val = fa.getFieldValue();
    if (ens.shouldFire()) {
        DataflowEvent e = new DataflowEvent(ens.getController(), this, val);
        // //            DataflowEvent e = new DataflowEvent(ens.getController(), this, access.toObject());
        ens.fireOut(e);
        // // the value might be altered
        val = e.getValue();
    }
    // if data==null this unconsumed @Out, its OK but we do not want to set it.
    if (data != null) {
        data.setFieldValue(val);
    }
    fa.out();
}
Also used : DataflowEvent(oms3.Notification.DataflowEvent)

Aggregations

ArrayList (java.util.ArrayList)4 Execute (oms3.annotations.Execute)4 File (java.io.File)3 IOException (java.io.IOException)3 Envelope (org.locationtech.jts.geom.Envelope)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 FileOutputStream (java.io.FileOutputStream)2 DataflowEvent (oms3.Notification.DataflowEvent)2 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)2 ModelsIOException (org.hortonmachine.gears.libs.exceptions.ModelsIOException)2 RegionMap (org.hortonmachine.gears.utils.RegionMap)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 Geometry (org.locationtech.jts.geom.Geometry)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 AffineTransform (java.awt.geom.AffineTransform)1 RenderedImage (java.awt.image.RenderedImage)1 WritableRaster (java.awt.image.WritableRaster)1 InputStream (java.io.InputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1