Skip to content

Commit

Permalink
multi sink issue (NRCangh-3), ocean source and sink (NRCangh-1), skel…
Browse files Browse the repository at this point in the history
…etonizer not enough verticies (NRCangh-2), direction d4 nodes (NRCangh-4)
  • Loading branch information
egouge committed Mar 16, 2021
1 parent 5135510 commit 648cbb1
Show file tree
Hide file tree
Showing 22 changed files with 329 additions and 129 deletions.
3 changes: 2 additions & 1 deletion chyf-catchment-delineator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>chyf-catchment-delineator</artifactId>
<name>CHyF Catchment Delineator</name>
<version>${catchment.version}</version>

<dependencies>
<dependency>
<groupId>net.refractions.chyf</groupId>
<artifactId>chyf-core</artifactId>
<version>${project.version}</version>
<version>${chyf.core.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
Expand Down
1 change: 1 addition & 0 deletions chyf-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<artifactId>chyf-core</artifactId>
<packaging>jar</packaging>
<version>${chyf.core.version}</version>
<name>CHyF Core Library</name>

<dependencies>
Expand Down
3 changes: 2 additions & 1 deletion chyf-flowpath-constructor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
<artifactId>chyf-flowpath-constructor</artifactId>
<packaging>jar</packaging>
<name>CHyF Flowpath Constructor</name>
<version>${flowpath.constructor.version}</version>

<dependencies>
<dependency>
<groupId>net.refractions.chyf</groupId>
<artifactId>chyf-core</artifactId>
<version>${project.version}</version>
<version>${chyf.core.version}</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.geotools.data.simple.SimpleFeatureReader;
import org.locationtech.jts.geom.Coordinate;
Expand All @@ -30,24 +29,23 @@
import org.locationtech.jts.geom.prep.PreparedPolygon;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.Name;
import org.opengis.filter.identity.FeatureId;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.refractions.chyf.datasource.ChyfAttribute;
import net.refractions.chyf.datasource.ChyfDataSource;
import net.refractions.chyf.datasource.DirectionType;
import net.refractions.chyf.datasource.EfType;
import net.refractions.chyf.datasource.FlowDirection;
import net.refractions.chyf.datasource.Layer;
import net.refractions.chyf.flowpathconstructor.ChyfProperties;
import net.refractions.chyf.flowpathconstructor.FlowpathArgs;
import net.refractions.chyf.flowpathconstructor.datasource.FlowpathGeoPackageDataSource;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.DEdge;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.DGraph;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.DNode;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.EdgeInfo;
import net.refractions.chyf.datasource.ChyfAttribute;
import net.refractions.chyf.datasource.ChyfDataSource;
import net.refractions.chyf.datasource.DirectionType;
import net.refractions.chyf.datasource.EfType;
import net.refractions.chyf.datasource.FlowDirection;
import net.refractions.chyf.datasource.Layer;

/**
* Main class for computing direction on dataset
Expand Down Expand Up @@ -119,20 +117,20 @@ public static void doWork(FlowpathGeoPackageDataSource dataSource, ChyfPropertie

//directionalized bank edges
logger.info("processing bank edges");
HashSet<Coordinate> nodec = new HashSet<>();
for(DNode d : graph.nodes) nodec.add(d.getCoordinate());
Set<FeatureId> bankstoflip = new HashSet<>();
for (EdgeInfo b : banks) {
if (nodec.contains(b.getEnd())) {
}else if (nodec.contains(b.getStart())) {
//flip
bankstoflip.add(b.getFeatureId());
}else {
throw new Exception("Bank flowpath does not intersect flow network: " + b.getStart());
}
}

dataSource.flipFlowEdges(bankstoflip, banks.stream().map(e->e.getFeatureId()).collect(Collectors.toList()));
// HashSet<Coordinate> nodec = new HashSet<>();
// for(DNode d : graph.nodes) nodec.add(d.getCoordinate());
// Set<FeatureId> bankstoflip = new HashSet<>();
// for (EdgeInfo b : banks) {
// if (nodec.contains(b.getEnd())) {
// }else if (nodec.contains(b.getStart())) {
// //flip
// bankstoflip.add(b.getFeatureId());
// }else {
// throw new Exception("Bank flowpath does not intersect flow network: " + b.getStart());
// }
// }
//
// dataSource.flipFlowEdges(bankstoflip, banks.stream().map(e->e.getFeatureId()).collect(Collectors.toList()));

//find sink nodes
logger.info("locating sink nodes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Set;

import org.geotools.filter.identity.FeatureIdImpl;
import org.locationtech.jts.geom.Coordinate;
import org.opengis.filter.identity.FeatureId;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
Expand All @@ -21,6 +22,7 @@
import net.refractions.chyf.flowpathconstructor.directionalize.graph.DGraph;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.DNode;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.DPath;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.EdgeInfo;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.Partition;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.PathDirectionalizer;
import net.refractions.chyf.flowpathconstructor.directionalize.graph.SubGraph;
Expand Down Expand Up @@ -123,7 +125,6 @@ private void directionalizeGraph(DGraph graph, DNode[] sinkNodes) throws Excepti
for (DNode n : sinkNodes) toProcess.add(n);

while(!toProcess.isEmpty()) {
//logger.info("Processing sink. Remaining: " + toProcess.size());

DNode sink = toProcess.remove(0);

Expand All @@ -141,6 +142,59 @@ private void directionalizeGraph(DGraph graph, DNode[] sinkNodes) throws Excepti
}
}


if (localSinks.size() > 1) {
//we need at least one degree 3 node in the network otherwise it is basically a loop
boolean degree3 = false;
for (DNode n : sub.getNodes()) {
if (n.getDegree() > 2) {
degree3 = true;
break;
}
}
if (degree3) {
//generally not an error, but I've seen a lot of direction errors
//in the dataset when this is the case so warn user
StringBuilder sb = new StringBuilder();
sb.append("A sub network has multiple sinks: " );
for (DNode n : localSinks) sb.append(n.toString() + " " );
logger.warn(sb.toString());

//add a single sink node and join all sinks to that node
Coordinate c = localSinks.get(0).getCoordinate();

DNode tempsink = new DNode(new Coordinate(c.x, c.y));
graph.getNodes().add(tempsink);
//link sink nodes to edges
for (DNode s : localSinks) {
DEdge temp = new DEdge(s,tempsink,s.getCoordinate(),tempsink.getCoordinate(),
new EdgeInfo(s.getCoordinate(), s.getCoordinate(), tempsink.getCoordinate(),
tempsink.getCoordinate(), EfType.REACH, new FeatureIdImpl("temp-sink-node"),
0, DirectionType.KNOWN));
tempsink.addEdge(temp);
s.getEdges().add(temp);
graph.getEdges().add(temp);
}

//the sink node needs to have only one in edge
DNode tempsink2 = new DNode(new Coordinate(c.x, c.y));
tempsink.setSink(true);
graph.getNodes().add(tempsink2);
DEdge temp = new DEdge(tempsink,tempsink2,tempsink.getCoordinate(),tempsink2.getCoordinate(),
new EdgeInfo(tempsink.getCoordinate(), tempsink.getCoordinate(), tempsink2.getCoordinate(),
tempsink2.getCoordinate(), EfType.REACH, new FeatureIdImpl("temp-sink-node2"), 0,
DirectionType.KNOWN));
tempsink.addEdge(temp);
tempsink2.getEdges().add(temp);
graph.getEdges().add(temp);


localSinks.clear();
localSinks.add(tempsink2);
sink = tempsink2;
sub = SubGraph.computeSubGraph(graph, sink);
}
}
//find bridge nodes
BridgeFinder bb = new BridgeFinder();
bb.computeBridges(sub, sink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public void dir(DGraph graph, Set<DNode> sinks, Set<DNode> sources) throws Excep
path.nodes.forEach(n->n.pathnode = true);
if (!path.edges.isEmpty()) {
directionalizePath(path);
path.edges.forEach(e->e.pathedge = true);
paths.add(path);
}
}else {
Expand All @@ -206,7 +207,6 @@ public void dir(DGraph graph, Set<DNode> sinks, Set<DNode> sources) throws Excep
if (temp == null ) {
temp = PathFinder.findPath(toprocess.getNodeB(), sinks, graph);
}
// DPath temp = PathFinder.findPath(toprocess.getNodeB(), sinks, graph);

if (temp == null) {
//not path found
Expand All @@ -215,13 +215,13 @@ public void dir(DGraph graph, Set<DNode> sinks, Set<DNode> sources) throws Excep
continue;
}
//flag nodes as pathnodes
temp.nodes.forEach(n->n.pathnode = true);


//add start node/edge to path
temp.nodes.add(0, toprocess.getNodeA());
temp.edges.add(0, toprocess);
directionalizePath(temp);

boolean canflip = true;
//check for cycle and flip is necessary
if (Directionalizer.cycleCheck(temp, graph)) {
Expand All @@ -231,10 +231,46 @@ public void dir(DGraph graph, Set<DNode> sinks, Set<DNode> sources) throws Excep
Collections.reverse(temp.nodes);
for (DEdge e : temp.edges) e.flip();
if (Directionalizer.cycleCheck(temp, graph)) {
throw new Exception("Cannot add path as both directions for this path creates a cycle. " + temp.toString());

//can we truncate this path
DPath temp2 = new DPath();
for (int i = 0; i < temp.edges.size(); i ++) {
if (temp.edges.get(i).getNodeB().pathnode) {
//stop here
temp2.getEdges().add(temp.getEdges().get(i));
if (i == 0) temp2.getNodes().add(temp.getEdges().get(i).getNodeA());
temp2.getNodes().add(temp.getEdges().get(i).getNodeB());
break;
}

temp2.getEdges().add(temp.getEdges().get(i));
if (i == 0) temp2.getNodes().add(temp.getEdges().get(i).getNodeA());
temp2.getNodes().add(temp.getEdges().get(i).getNodeB());


}
if (temp.edges.size() > temp2.edges.size()) {
for (DEdge e : temp.edges) {
if (!temp2.edges.contains(e)) {
e.resetKnown();
}
}
if (Directionalizer.cycleCheck(temp2, graph)) {
throw new Exception("Cannot add path as both directions for this path creates a cycle. " + temp.toString());
}else {
System.out.println("USING TRUNCATED PATH");
temp = temp2;
}

}else {
throw new Exception("Cannot add path as both directions for this path creates a cycle. " + temp.toString());
}
}
}

temp.nodes.forEach(n->n.pathnode = true);
temp.edges.forEach(e->e.pathedge = true);

if (canflip) {

//can go either way without cycle
Expand Down Expand Up @@ -413,7 +449,7 @@ private void directionalizePath(DPath path) throws Exception{
}else {
path.edges.get(k).setKnown();
}
path.edges.get(k).pathedge = true;
// path.edges.get(k).pathedge = true;
}
}

Expand Down Expand Up @@ -442,6 +478,7 @@ public DPath findStraightestPath(DEdge inEdge, Set<DNode> sinks, DGraph graph) t
double mangle = Double.MAX_VALUE;
DEdge lnextEdge = null;
DNode lnextNode = null;

nextNode.pathvisited = true;
for (DEdge out : nextNode.getEdges()) {
if (out.getDType() == DirectionType.KNOWN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ public void directionalize(DGraph graph, List<DNode> localSinks) throws Exceptio
break;
}
if (e.getNodeB() != sink) {
e.flip();
if (localSinks.contains(e.getNodeB())) {
logger.error("Potential loop detected at " + sink.toString() + " - both ends of this subnetwork are classified as sinks.");
break;
}else {
e.flip();
}
}else {
e.setKnown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static void doWork(FlowpathGeoPackageDataSource dataSource, ChyfPropertie

logger.info("POINT GENERATOR: " + cnt);
cnt++;

ftouch.clear();
ptouch.clear();

Expand Down
Loading

0 comments on commit 648cbb1

Please sign in to comment.