From 7366f6fb8097243f2ff88f39e08aebc267fa5d2f Mon Sep 17 00:00:00 2001 From: Shivam Date: Sun, 5 Mar 2023 00:31:08 +0530 Subject: [PATCH 1/5] create custom safe zones with shapefile --- .../EvacuationNetworkGenerator.java | 99 +++++++++++++++++-- 1 file changed, 93 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java index 5909498..819317c 100644 --- a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java +++ b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java @@ -20,10 +20,13 @@ package org.matsim.evacuationgui.scenariogenerator; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.List; import org.apache.log4j.Logger; +import org.geotools.geometry.jts.JTS; +import org.geotools.referencing.CRS; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Point; @@ -36,6 +39,9 @@ import org.matsim.api.core.v01.network.Node; import org.matsim.core.network.algorithms.NetworkCleaner; import org.matsim.core.utils.geometry.geotools.MGC; +import org.opengis.feature.simple.SimpleFeature; +import org.opengis.referencing.FactoryException; +import org.opengis.referencing.operation.TransformException; /** @@ -47,27 +53,42 @@ public class EvacuationNetworkGenerator { private static final Logger log = Logger.getLogger(EvacuationNetworkGenerator.class); - private final Geometry evacuationArea; - private final Network network; + private Geometry evacuationArea; + + private final Collection safePoints; + private final String fromSafePointSystem; + private final String toSafePointSystem; + private Network network; private final HashSet redundantLinks = new HashSet(); private final HashSet safeNodes = new HashSet(); private final HashSet redundantNodes = new HashSet(); - private final Id safeNodeAId; + private Id safeNodeAId; + + private Id safeNodeBId; - private final Id safeNodeBId; + private Id safeLinkId; - private final Id safeLinkId; + public EvacuationNetworkGenerator(Scenario sc, Geometry evavcuationArea, Id safeLinkId, Collection safePoints, String fromSafePointSystem, String toSafePointSystem) { + this.evacuationArea = evavcuationArea;//.buffer(4000); + this.network = sc.getNetwork(); + this.safePoints = safePoints; + this.fromSafePointSystem = fromSafePointSystem; + this.toSafePointSystem = toSafePointSystem; +// this.safeNodeAId = Id.create("en1", Node.class); +// this.safeNodeBId = Id.create("en2", Node.class); + this.safeLinkId = safeLinkId; + } public EvacuationNetworkGenerator(Scenario sc, Geometry evavcuationArea, Id safeLinkId) { + this(sc, evavcuationArea, safeLinkId, null, "", ""); this.evacuationArea = evavcuationArea;//.buffer(4000); this.network = sc.getNetwork(); this.safeNodeAId = Id.create("en1", Node.class); this.safeNodeBId = Id.create("en2", Node.class); this.safeLinkId = safeLinkId; } - public void run() { log.info("generating evacuation net ..."); log.info("pre-cleaning network"); @@ -81,6 +102,19 @@ public void run() { log.info("done."); } + public void run(Collection safePoints, String fromSafePointSystem, String toSafePointSystem) { + log.info("generating evacuation net ..."); + log.info("pre-cleaning network"); + preClean(); + log.info("classifing nodes"); + classifyNodesAndLinks(); + log.info("creating evacuation nodes and links"); + createEvacuationNodsAndLinks(safePoints, fromSafePointSystem, toSafePointSystem); + log.info("removing links and nodes that are outside the evacuation area"); + //cleanUpNetwork(); + log.info("done."); + } + private void preClean() { log.info("Pre-cleanup #nodes: " + this.network.getNodes().size()); List rm = new ArrayList<>(); @@ -139,6 +173,59 @@ private void createEvacuationNodsAndLinks() { } } + private void createEvacuationNodsAndLinks(Collection safePoints, String fromSystem, String toSystem) { + for (SimpleFeature safePoint : safePoints) { + Geometry safePointDefaultGeometry = (Geometry) safePoint.getDefaultGeometry(); + + Geometry transformedSafePoint; + try { + transformedSafePoint = JTS.transform(safePointDefaultGeometry, CRS.findMathTransform(MGC.getCRS(fromSystem), MGC.getCRS(toSystem), true)); + } catch (TransformException | FactoryException e) { + throw new RuntimeException("Transformation isn't successful" + e); + } + + Coordinate cc1 = transformedSafePoint.getCoordinate(); + Coord safeCoord1 = MGC.coordinate2Coord(cc1); + + Coordinate cc2 = transformedSafePoint.getCoordinate(); + cc2.x += 10; + cc2.y += 10; + Coord safeCoord2 = MGC.coordinate2Coord(cc2); + + this.safeNodeAId = Id.createNodeId("en1_" + transformedSafePoint.getCoordinate().x); + this.safeNodeBId = Id.createNodeId("en2_" + transformedSafePoint.getCoordinate().y); + + Node safeNodeA = this.network.getFactory().createNode(this.safeNodeAId, safeCoord1); + this.network.addNode(safeNodeA); + Node safeNodeB = this.network.getFactory().createNode(this.safeNodeBId, safeCoord2); + this.network.addNode(safeNodeB); + + double capacity = 1000000.; + this.safeLinkId = Id.createLinkId(safeLinkId.toString() + "_" + transformedSafePoint.getCoordinate().y); + Link l = this.network.getFactory().createLink(this.safeLinkId, safeNodeA, safeNodeB); + l.setLength(10); + l.setFreespeed(100000); + l.setCapacity(capacity); + l.setNumberOfLanes(100); + this.network.addLink(l); + + int linkId = 1; + for (Node node : this.network.getNodes().values()) { + Id nodeId = node.getId(); + // && !nodeId.equals(this.safeNodeAId) && !nodeId.equals(this.safeNodeBId)) { + if (this.safeNodes.contains(node) && !nodeId.equals(this.safeNodeAId) && !nodeId.equals(this.safeNodeBId)) { + String sLinkID = "el" + linkId++ + transformedSafePoint.getCoordinate().x; + Link l2 = this.network.getFactory().createLink(Id.create(sLinkID, Link.class), node, safeNodeA); + l2.setLength(10); + l2.setFreespeed(100000); + l2.setCapacity(capacity); + l2.setNumberOfLanes(1); + this.network.addLink(l2); + } + } + } + } + private void classifyNodesAndLinks() { for (Node node : this.network.getNodes().values()) { From 6ff121f36e296a80ce0f2ba0bab3bd590ee95860 Mon Sep 17 00:00:00 2001 From: Shivam Date: Thu, 9 Mar 2023 19:36:55 +0530 Subject: [PATCH 2/5] added modeshare od values, and auto mode --- .../scenariogenerator/EvacuationNetworkGenerator.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java index 819317c..0c684d7 100644 --- a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java +++ b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java @@ -174,6 +174,7 @@ private void createEvacuationNodsAndLinks() { } private void createEvacuationNodsAndLinks(Collection safePoints, String fromSystem, String toSystem) { + int counter = 1; for (SimpleFeature safePoint : safePoints) { Geometry safePointDefaultGeometry = (Geometry) safePoint.getDefaultGeometry(); @@ -185,11 +186,13 @@ private void createEvacuationNodsAndLinks(Collection safePoints, } Coordinate cc1 = transformedSafePoint.getCoordinate(); + cc1.x += 10000; + cc1.y += 0; Coord safeCoord1 = MGC.coordinate2Coord(cc1); Coordinate cc2 = transformedSafePoint.getCoordinate(); - cc2.x += 10; - cc2.y += 10; + cc2.x += 10010; + cc2.y += 0; Coord safeCoord2 = MGC.coordinate2Coord(cc2); this.safeNodeAId = Id.createNodeId("en1_" + transformedSafePoint.getCoordinate().x); @@ -201,7 +204,7 @@ private void createEvacuationNodsAndLinks(Collection safePoints, this.network.addNode(safeNodeB); double capacity = 1000000.; - this.safeLinkId = Id.createLinkId(safeLinkId.toString() + "_" + transformedSafePoint.getCoordinate().y); + this.safeLinkId = Id.createLinkId(safeLinkId.toString() + "_" + counter++); Link l = this.network.getFactory().createLink(this.safeLinkId, safeNodeA, safeNodeB); l.setLength(10); l.setFreespeed(100000); From 0b922e886128cadd7e6498810a39a24bc962eb0d Mon Sep 17 00:00:00 2001 From: Shivam Date: Sun, 12 Mar 2023 03:50:52 +0530 Subject: [PATCH 3/5] map with safe link id and safe point are used --- .../EvacuationNetworkGenerator.java | 57 ++++--------------- 1 file changed, 11 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java index 0c684d7..d876d47 100644 --- a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java +++ b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java @@ -19,14 +19,9 @@ * *********************************************************************** */ package org.matsim.evacuationgui.scenariogenerator; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; +import java.util.*; import org.apache.log4j.Logger; -import org.geotools.geometry.jts.JTS; -import org.geotools.referencing.CRS; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Point; @@ -40,8 +35,6 @@ import org.matsim.core.network.algorithms.NetworkCleaner; import org.matsim.core.utils.geometry.geotools.MGC; import org.opengis.feature.simple.SimpleFeature; -import org.opengis.referencing.FactoryException; -import org.opengis.referencing.operation.TransformException; /** @@ -55,9 +48,6 @@ public class EvacuationNetworkGenerator { private Geometry evacuationArea; - private final Collection safePoints; - private final String fromSafePointSystem; - private final String toSafePointSystem; private Network network; private final HashSet redundantLinks = new HashSet(); @@ -70,19 +60,7 @@ public class EvacuationNetworkGenerator { private Id safeLinkId; - public EvacuationNetworkGenerator(Scenario sc, Geometry evavcuationArea, Id safeLinkId, Collection safePoints, String fromSafePointSystem, String toSafePointSystem) { - this.evacuationArea = evavcuationArea;//.buffer(4000); - this.network = sc.getNetwork(); - this.safePoints = safePoints; - this.fromSafePointSystem = fromSafePointSystem; - this.toSafePointSystem = toSafePointSystem; -// this.safeNodeAId = Id.create("en1", Node.class); -// this.safeNodeBId = Id.create("en2", Node.class); - this.safeLinkId = safeLinkId; - } - public EvacuationNetworkGenerator(Scenario sc, Geometry evavcuationArea, Id safeLinkId) { - this(sc, evavcuationArea, safeLinkId, null, "", ""); this.evacuationArea = evavcuationArea;//.buffer(4000); this.network = sc.getNetwork(); this.safeNodeAId = Id.create("en1", Node.class); @@ -102,14 +80,14 @@ public void run() { log.info("done."); } - public void run(Collection safePoints, String fromSafePointSystem, String toSafePointSystem) { + public void run(Map, Geometry> safePoints) { log.info("generating evacuation net ..."); log.info("pre-cleaning network"); preClean(); log.info("classifing nodes"); classifyNodesAndLinks(); log.info("creating evacuation nodes and links"); - createEvacuationNodsAndLinks(safePoints, fromSafePointSystem, toSafePointSystem); + createEvacuationNodsAndLinks(safePoints); log.info("removing links and nodes that are outside the evacuation area"); //cleanUpNetwork(); log.info("done."); @@ -173,30 +151,17 @@ private void createEvacuationNodsAndLinks() { } } - private void createEvacuationNodsAndLinks(Collection safePoints, String fromSystem, String toSystem) { - int counter = 1; - for (SimpleFeature safePoint : safePoints) { - Geometry safePointDefaultGeometry = (Geometry) safePoint.getDefaultGeometry(); - - Geometry transformedSafePoint; - try { - transformedSafePoint = JTS.transform(safePointDefaultGeometry, CRS.findMathTransform(MGC.getCRS(fromSystem), MGC.getCRS(toSystem), true)); - } catch (TransformException | FactoryException e) { - throw new RuntimeException("Transformation isn't successful" + e); - } + private void createEvacuationNodsAndLinks(Map, Geometry> safePoints) { + for (Map.Entry, Geometry> safePoint: safePoints.entrySet()) { - Coordinate cc1 = transformedSafePoint.getCoordinate(); - cc1.x += 10000; - cc1.y += 0; + Coordinate cc1 = safePoint.getValue().getCoordinate(); Coord safeCoord1 = MGC.coordinate2Coord(cc1); - Coordinate cc2 = transformedSafePoint.getCoordinate(); - cc2.x += 10010; - cc2.y += 0; + Coordinate cc2 = safePoint.getValue().getCoordinate(); Coord safeCoord2 = MGC.coordinate2Coord(cc2); - this.safeNodeAId = Id.createNodeId("en1_" + transformedSafePoint.getCoordinate().x); - this.safeNodeBId = Id.createNodeId("en2_" + transformedSafePoint.getCoordinate().y); + this.safeNodeAId = Id.createNodeId("en1_" + safePoint.getValue().getCoordinate()); + this.safeNodeBId = Id.createNodeId("en2_" + safePoint.getValue().getCoordinate()); Node safeNodeA = this.network.getFactory().createNode(this.safeNodeAId, safeCoord1); this.network.addNode(safeNodeA); @@ -204,7 +169,7 @@ private void createEvacuationNodsAndLinks(Collection safePoints, this.network.addNode(safeNodeB); double capacity = 1000000.; - this.safeLinkId = Id.createLinkId(safeLinkId.toString() + "_" + counter++); + this.safeLinkId = safePoint.getKey(); Link l = this.network.getFactory().createLink(this.safeLinkId, safeNodeA, safeNodeB); l.setLength(10); l.setFreespeed(100000); @@ -217,7 +182,7 @@ private void createEvacuationNodsAndLinks(Collection safePoints, Id nodeId = node.getId(); // && !nodeId.equals(this.safeNodeAId) && !nodeId.equals(this.safeNodeBId)) { if (this.safeNodes.contains(node) && !nodeId.equals(this.safeNodeAId) && !nodeId.equals(this.safeNodeBId)) { - String sLinkID = "el" + linkId++ + transformedSafePoint.getCoordinate().x; + String sLinkID = "el_" + linkId++ + "_" + this.safeLinkId; Link l2 = this.network.getFactory().createLink(Id.create(sLinkID, Link.class), node, safeNodeA); l2.setLength(10); l2.setFreespeed(100000); From d018dc17753f15792d80ed5d7abe97b46e27f147 Mon Sep 17 00:00:00 2001 From: Shivam Date: Sun, 12 Mar 2023 10:43:22 +0530 Subject: [PATCH 4/5] cleannetwork now keeps all safepoints --- .../EvacuationNetworkGenerator.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java index d876d47..b9c47de 100644 --- a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java +++ b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java @@ -58,6 +58,8 @@ public class EvacuationNetworkGenerator { private Id safeNodeBId; + private final Collection> safeNodeBIds = new ArrayList<>(); + private Id safeLinkId; public EvacuationNetworkGenerator(Scenario sc, Geometry evavcuationArea, Id safeLinkId) { @@ -89,7 +91,7 @@ public void run(Map, Geometry> safePoints) { log.info("creating evacuation nodes and links"); createEvacuationNodsAndLinks(safePoints); log.info("removing links and nodes that are outside the evacuation area"); - //cleanUpNetwork(); + cleanUpNetwork(); log.info("done."); } @@ -126,6 +128,7 @@ private void createEvacuationNodsAndLinks() { this.network.addNode(safeNodeA); Node safeNodeB = this.network.getFactory().createNode(this.safeNodeBId, safeCoord2); this.network.addNode(safeNodeB); + this.safeNodeBIds.add(this.safeNodeBId); double capacity = 1000000.; Link l = this.network.getFactory().createLink(this.safeLinkId, safeNodeA, safeNodeB); @@ -167,6 +170,7 @@ private void createEvacuationNodsAndLinks(Map, Geometry> safePoints) { this.network.addNode(safeNodeA); Node safeNodeB = this.network.getFactory().createNode(this.safeNodeBId, safeCoord2); this.network.addNode(safeNodeB); + this.safeNodeBIds.add(safeNodeBId); double capacity = 1000000.; this.safeLinkId = safePoint.getKey(); @@ -285,12 +289,14 @@ protected void cleanUpNetwork() { log.info("adding dummy links"); List dummies = new ArrayList(); int dCnt = 0; - for (Node n : this.network.getNodes().values()) { - if (!this.safeNodes.contains(n) && !this.redundantNodes.contains(n)) { - NetworkFactory fac = this.network.getFactory(); - Link l = fac.createLink(Id.create("dummy" + dCnt++, Link.class), this.network.getNodes().get(this.safeNodeBId), n); - this.network.addLink(l); - dummies.add(l); + for (Id safeNodeBIdFromCollection: this.safeNodeBIds) { + for (Node n : this.network.getNodes().values()) { + if (!this.safeNodes.contains(n) && !this.redundantNodes.contains(n)) { + NetworkFactory fac = this.network.getFactory(); + Link l = fac.createLink(Id.create("dummy" + dCnt++, Link.class), this.network.getNodes().get(safeNodeBIdFromCollection), n); + this.network.addLink(l); + dummies.add(l); + } } } new NetworkCleaner().run(this.network); From 5fe141ebcf2c22a44b4f7e48e4b03b80533c163d Mon Sep 17 00:00:00 2001 From: Shivam Date: Sun, 12 Mar 2023 11:23:44 +0530 Subject: [PATCH 5/5] need to extract safeNodeAIds and use safeNodeBIds --- .../scenariogenerator/EvacuationNetworkGenerator.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java index b9c47de..1615248 100644 --- a/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java +++ b/src/main/java/org/matsim/evacuationgui/scenariogenerator/EvacuationNetworkGenerator.java @@ -60,6 +60,11 @@ public class EvacuationNetworkGenerator { private final Collection> safeNodeBIds = new ArrayList<>(); + public Collection> getSafeNodeAIds() { + return safeNodeAIds; + } + + private final Collection> safeNodeAIds = new ArrayList<>(); private Id safeLinkId; public EvacuationNetworkGenerator(Scenario sc, Geometry evavcuationArea, Id safeLinkId) { @@ -126,6 +131,7 @@ private void createEvacuationNodsAndLinks() { Node safeNodeA = this.network.getFactory().createNode(this.safeNodeAId, safeCoord1); this.network.addNode(safeNodeA); + this.safeNodeAIds.add(this.safeNodeAId); Node safeNodeB = this.network.getFactory().createNode(this.safeNodeBId, safeCoord2); this.network.addNode(safeNodeB); this.safeNodeBIds.add(this.safeNodeBId); @@ -168,6 +174,7 @@ private void createEvacuationNodsAndLinks(Map, Geometry> safePoints) { Node safeNodeA = this.network.getFactory().createNode(this.safeNodeAId, safeCoord1); this.network.addNode(safeNodeA); + this.safeNodeAIds.add(this.safeNodeAId); Node safeNodeB = this.network.getFactory().createNode(this.safeNodeBId, safeCoord2); this.network.addNode(safeNodeB); this.safeNodeBIds.add(safeNodeBId);