Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 94 additions & 62 deletions src/main/java/net/refractions/chyf/ChyfDatastore.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.PrecisionModel;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class ChyfDatastore {
static final Logger logger = LoggerFactory.getLogger(ChyfDatastore.class.getCanonicalName());

public static final int BASE_SRS = 6624; // Quebec Albers
public static GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(new PrecisionModel(), BASE_SRS);
public static final int MAX_RESULTS = 20000;
public static final int PRECISION_MODEL = 1000;
public static GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(new PrecisionModel(PRECISION_MODEL), BASE_SRS);

private HyGraph hyGraph;

Expand All @@ -67,74 +69,20 @@ public HyGraph getHyGraph() {

private void init() {
try {
GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(new PrecisionModel(1000), BASE_SRS);

HyGraphBuilder gb = new HyGraphBuilder();

@SuppressWarnings("resource")
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringJdbcConfiguration.class); //ML
WKTReader wktreader = new WKTReader();

// read and add Waterbodies
logger.info("Reading waterbodies");
WaterbodyDAO waterbodyDAO = (WaterbodyDAO) context.getBean(WaterbodyDAO.class);

for(Waterbody wb : waterbodyDAO.getWaterbodies()) {
Geometry waterCatchment = GEOMETRY_FACTORY.createGeometry(wktreader.read(wb.getLinestring()));
CatchmentType type = CatchmentType.UNKNOWN;
switch(wb.getDefinition()) {
case 1:
type = CatchmentType.WATER_CANAL;
break;
case 4:
type = CatchmentType.WATER_LAKE;
break;
case 6:
type = CatchmentType.WATER_RIVER;
break;
case 9:
type = CatchmentType.WATER_POND;
break;
}
gb.addECatchment(type, (Polygon)waterCatchment);
}
// read and create Waterbodies
List<Waterbody> waterbodies = read(Waterbody.class);
createWaterbodies(waterbodies, gb);

// read and add Catchments
logger.info("Reading catchments");
CatchmentDAO catchmentDAO = (CatchmentDAO) context.getBean(CatchmentDAO.class);
List<Catchment> catchments = read(Catchment.class);
createCatchments(catchments, gb);

List<Catchment> catchments = catchmentDAO.getCatchments();
for (Catchment c : catchments) {
Geometry catchment = GEOMETRY_FACTORY.createGeometry(wktreader.read(c.getLinestring()));
gb.addECatchment(CatchmentType.UNKNOWN, (Polygon)catchment);
}

// read and add Flowpaths
logger.info("Reading flowpaths");
FlowpathDAO flow = (FlowpathDAO) context.getBean(FlowpathDAO.class);

List<Flowpath> flowpaths = flow.getFlowpaths();
for (Flowpath fp : flowpaths){
Geometry flowPath = GEOMETRY_FACTORY.createGeometry(wktreader.read(fp.getLinestring()));
FlowpathType type = FlowpathType.convert(fp.getType()); //ML
String rankString = fp.getRank(); //ML
int rank = -1;
if(rankString.equals("Primary")) {
rank = 1;
} else if(rankString.equals("Secondary")) {
rank = 2;
}
String name = fp.getName().intern(); //ML
UUID nameId = null;
try {
nameId = UuidUtil.UuidFromString(fp.getNameId()); //ML
} catch(IllegalArgumentException iae) {
logger.warn("Exception reading UUID: " + iae.getMessage());
}
Integer certainty = fp.getCertainty();
gb.addEFlowpath(type, rank, name, nameId, certainty, (LineString)flowPath);

}
List<Flowpath> flowpaths = read(Flowpath.class);
createFlowpaths(flowpaths, gb);

hyGraph = gb.build();

Expand All @@ -143,6 +91,90 @@ private void init() {
}
}

@SuppressWarnings("unchecked")
public <T> List<T> read(Class<T> clazz) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringJdbcConfiguration.class); //ML

if(clazz.equals(Waterbody.class)) {
logger.info("Reading waterbodies");
WaterbodyDAO waterbodyDAO = (WaterbodyDAO) context.getBean(WaterbodyDAO.class);
return (List<T>) waterbodyDAO.getWaterbodies();
}
else if (clazz.equals(Catchment.class)) {
logger.info("Reading catchments");
CatchmentDAO catchmentDAO = (CatchmentDAO) context.getBean(CatchmentDAO.class);
return (List<T>) catchmentDAO.getCatchments();
}
else if (clazz.equals(Flowpath.class)) {
logger.info("Reading flowpaths");
FlowpathDAO flowpathDAO = (FlowpathDAO) context.getBean(FlowpathDAO.class);
return (List<T>) flowpathDAO.getFlowpaths();
}
return null;

}

public void createWaterbodies(List<Waterbody> waterbodies, HyGraphBuilder gb) throws ParseException {
logger.info("Creating waterbodies");
WKTReader wktreader = new WKTReader();

for(Waterbody wb : waterbodies) {
Geometry waterCatchment = GEOMETRY_FACTORY.createGeometry(wktreader.read(wb.getLinestring()));
CatchmentType type = CatchmentType.UNKNOWN;
switch(wb.getDefinition()) {
case 1:
type = CatchmentType.WATER_CANAL;
break;
case 4:
type = CatchmentType.WATER_LAKE;
break;
case 6:
type = CatchmentType.WATER_RIVER;
break;
case 9:
type = CatchmentType.WATER_POND;
break;
}
gb.addECatchment(type, (Polygon)waterCatchment);
}
}

public void createCatchments(List<Catchment> catchments, HyGraphBuilder gb) throws ParseException {
logger.info("Creating catchments");
WKTReader wktreader = new WKTReader();

for (Catchment c : catchments) {
Geometry catchment = GEOMETRY_FACTORY.createGeometry(wktreader.read(c.getLinestring()));
gb.addECatchment(CatchmentType.UNKNOWN, (Polygon)catchment);
}
}

public void createFlowpaths(List<Flowpath> flowpaths, HyGraphBuilder gb) throws ParseException {
logger.info("Creating flowpaths");
WKTReader wktreader = new WKTReader();

for (Flowpath fp : flowpaths){
Geometry flowPath = GEOMETRY_FACTORY.createGeometry(wktreader.read(fp.getLinestring()));
FlowpathType type = FlowpathType.convert(fp.getType()); //ML
String rankString = fp.getRank(); //ML
int rank = -1;
if(rankString.equals("Primary")) {
rank = 1;
} else if(rankString.equals("Secondary")) {
rank = 2;
}
String name = fp.getName().intern(); //ML
UUID nameId = null;
try {
nameId = UuidUtil.UuidFromString(fp.getNameId()); //ML
} catch(IllegalArgumentException iae) {
logger.warn("Exception reading UUID: " + iae.getMessage());
}
Integer certainty = fp.getCertainty();
gb.addEFlowpath(type, rank, name, nameId, certainty, (LineString)flowPath);
}
}

private void init(String dataDir) {
try {
// the shapefile data is actually in 4617 CSRS/GRS80/NAD83
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/refractions/chyf/hygraph/HyGraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,32 @@ public Nexus addNexus(Point point) {
nexusIndex.insert(node.getPoint().getEnvelopeInternal(),node);
return node;
}

public void clearCatchments() {
this.eCatchments.clear();
nextCatchmentId = 1;
this.eCatchmentIndex = new Quadtree();
}

public void clearFlowpaths() {
this.eFlowpaths.clear();
nextNexusId = 1;
this.eCatchmentIndex = new Quadtree();
}


public List<EFlowpath> getEFlowpaths() {
return eFlowpaths;
}

public List<Nexus> getNexuses() {
return nexuses;
}


public List<ECatchment> getECatchments() {
return eCatchments;
}

private void classifyNexuses() {
for(Nexus n : nexuses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@

import net.refractions.chyf.enumTypes.FlowpathType;
import net.refractions.chyf.enumTypes.NexusType;
import net.refractions.util.StopWatch;

public class StreamOrderCalculator {
static final Logger logger = LoggerFactory.getLogger(StreamOrderCalculator.class.getCanonicalName());

public static void calcOrders(List<EFlowpath> eFlowpaths, List<Nexus> nexuses) {
logger.info("Calculating Stream Orders");
StopWatch sw = new StopWatch();
sw.start();

// reset all stream orders to null everywhere
for(EFlowpath f : eFlowpaths) {
Expand All @@ -35,10 +32,7 @@ public static void calcOrders(List<EFlowpath> eFlowpaths, List<Nexus> nexuses) {
mf.assignHackOrder(1);
}
}
}

sw.stop();
logger.info("Stream Orders calculated in " + sw.getElapsedTime() + "ms");
}
}

private static Integer calcStrahlerOrder(EFlowpath f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ApiResponse {

private Object data;
private int srs;
private long executionTime;
private double executionTime;
private String errorMsg;
private String callback = "jsonp";
private Double scale = null;
Expand Down Expand Up @@ -54,11 +54,11 @@ public String getCallback() {
return callback;
}

public void setExecutionTime(long executionTime) {
public void setExecutionTime(double executionTime) {
this.executionTime = executionTime;
}

public long getExecutionTime() {
public double getExecutionTime() {
return executionTime;
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/refractions/util/StopWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public void stop() {
* Returns elapsed time in milliseconds
* @return elapsed time in milliseconds
*/
public long getElapsedTime() {
long elapsed;
public double getElapsedTime() {
double elapsed;
if(running) {
elapsed = (System.nanoTime() - startTime) / 1000000;
elapsed = (System.nanoTime() - startTime) / 1000000.0;
}
else {
elapsed = (stopTime - startTime) / 1000000;
elapsed = (stopTime - startTime) / 1000000.0;
}
return elapsed;
}
Expand All @@ -35,8 +35,8 @@ public long getElapsedTime() {
* Returns elapsed time in seconds
* @return elapsed time in seconds
*/
public long getElapsedTimeSecs() {
return getElapsedTime() / 1000;
public double getElapsedTimeSecs() {
return getElapsedTime() / 1000.0;
}

// sample usage
Expand Down
Loading