diff --git a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSV.java b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSV.java index fce5aa2bd66..7939a9dab9a 100644 --- a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSV.java +++ b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSV.java @@ -27,6 +27,7 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.Scenario; import org.matsim.api.core.v01.network.Link; +import org.matsim.application.options.ShpOptions; import org.matsim.freight.carriers.*; import org.matsim.freight.carriers.CarrierCapabilities.FleetSize; import org.matsim.core.utils.geometry.CoordinateTransformation; @@ -236,20 +237,20 @@ public void setFixedNumberOfVehiclePerTypeAndLocation(int fixedNumberOfVehiclePe * @param scenario * @param freightCarriersConfigGroup * @param csvLocationCarrier - * @param polygonsInShape + * @param indexShape * @param defaultJspritIterations * @param crsTransformationNetworkAndShape * @param shapeCategory * @throws IOException */ public static void readAndCreateCarrierFromCSV(Scenario scenario, FreightCarriersConfigGroup freightCarriersConfigGroup, - Path csvLocationCarrier, Collection polygonsInShape, int defaultJspritIterations, + Path csvLocationCarrier, ShpOptions.Index indexShape, int defaultJspritIterations, CoordinateTransformation crsTransformationNetworkAndShape, String shapeCategory) throws IOException { Set allNewCarrierInformation = readCarrierInformation(csvLocationCarrier); - checkNewCarrier(allNewCarrierInformation, freightCarriersConfigGroup, scenario, polygonsInShape, shapeCategory); + checkNewCarrier(allNewCarrierInformation, freightCarriersConfigGroup, scenario, indexShape, shapeCategory); log.info("The read carrier information from the csv are checked without errors."); - createNewCarrierAndAddVehicleTypes(scenario, allNewCarrierInformation, freightCarriersConfigGroup, polygonsInShape, + createNewCarrierAndAddVehicleTypes(scenario, allNewCarrierInformation, freightCarriersConfigGroup, indexShape, defaultJspritIterations, crsTransformationNetworkAndShape); } @@ -307,11 +308,11 @@ else if (!record.get("fleetSize").isBlank()) * @param allNewCarrierInformation * @param freightCarriersConfigGroup * @param scenario - * @param polygonsInShape + * @param indexShape * @param shapeCategory */ static void checkNewCarrier(Set allNewCarrierInformation, - FreightCarriersConfigGroup freightCarriersConfigGroup, Scenario scenario, Collection polygonsInShape, String shapeCategory) { + FreightCarriersConfigGroup freightCarriersConfigGroup, Scenario scenario, ShpOptions.Index indexShape, String shapeCategory) { CarriersUtils.addOrGetCarriers(scenario); for (CarrierInformationElement carrierElement : allNewCarrierInformation) { @@ -355,12 +356,12 @@ static void checkNewCarrier(Set allNewCarrierInformat log.warn( "No possible area for additional depot given. Random choice in the hole network of a possible position"); if (carrierElement.getAreaOfAdditionalDepots() != null) { - if (polygonsInShape == null) + if (indexShape == null) throw new RuntimeException("For carrier " + carrierElement.getName() + " a certain area for depots is selected, but no shape is read in. Please check."); for (String depotArea : carrierElement.getAreaOfAdditionalDepots()) { boolean isInShape = false; - for (SimpleFeature singlePolygon : polygonsInShape) { + for (SimpleFeature singlePolygon : indexShape.getShp().readFeatures()) { if (singlePolygon.getAttribute(shapeCategory).equals(depotArea)) { isInShape = true; break; @@ -409,14 +410,14 @@ static void checkNewCarrier(Set allNewCarrierInformat * @param scenario * @param allNewCarrierInformation * @param freightCarriersConfigGroup - * @param polygonsInShape + * @param indexShape * @param defaultJspritIterations * @param crsTransformationNetworkAndShape */ static void createNewCarrierAndAddVehicleTypes(Scenario scenario, - Set allNewCarrierInformation, FreightCarriersConfigGroup freightCarriersConfigGroup, - Collection polygonsInShape, int defaultJspritIterations, - CoordinateTransformation crsTransformationNetworkAndShape) { + Set allNewCarrierInformation, FreightCarriersConfigGroup freightCarriersConfigGroup, + ShpOptions.Index indexShape, int defaultJspritIterations, + CoordinateTransformation crsTransformationNetworkAndShape) { Carriers carriers = CarriersUtils.addOrGetCarriers(scenario); CarrierVehicleTypes carrierVehicleTypes = new CarrierVehicleTypes(); @@ -457,7 +458,7 @@ static void createNewCarrierAndAddVehicleTypes(Scenario scenario, && !link.getId().toString().contains("pt") && (!link.getAttributes().getAsMap().containsKey("type") || !link.getAttributes().getAsMap().get("type").toString().contains("motorway")) - && FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, + && FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, singleNewCarrier.getAreaOfAdditionalDepots(), crsTransformationNetworkAndShape)) { singleNewCarrier.getVehicleDepots().add(link.getId().toString()); } diff --git a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java index 83fb139cbdc..e9b09dd85d0 100644 --- a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java +++ b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java @@ -30,11 +30,11 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Population; +import org.matsim.application.options.ShpOptions; import org.matsim.core.network.NetworkUtils; import org.matsim.core.population.PopulationUtils; import org.matsim.core.router.TripStructureUtils; import org.matsim.core.utils.geometry.CoordinateTransformation; -import org.matsim.core.utils.geometry.geotools.MGC; import org.matsim.freight.carriers.*; import org.opengis.feature.simple.SimpleFeature; @@ -318,7 +318,7 @@ public String getTypeOfDemand() { * * @param scenario * @param csvLocationDemand - * @param polygonsInShape + * @param indexShape * @param combineSimilarJobs * @param crsTransformationNetworkAndShape * @param population @@ -326,12 +326,12 @@ public String getTypeOfDemand() { * @throws IOException */ static void readAndCreateDemand(Scenario scenario, Path csvLocationDemand, - Collection polygonsInShape, boolean combineSimilarJobs, + ShpOptions.Index indexShape, boolean combineSimilarJobs, CoordinateTransformation crsTransformationNetworkAndShape, Population population, String shapeCategory) throws IOException { Set demandInformation = readDemandInformation(csvLocationDemand); - checkNewDemand(scenario, demandInformation, polygonsInShape, shapeCategory); - createDemandForCarriers(scenario, polygonsInShape, demandInformation, population, combineSimilarJobs, + checkNewDemand(scenario, demandInformation, indexShape, shapeCategory); + createDemandForCarriers(scenario, indexShape, demandInformation, population, combineSimilarJobs, crsTransformationNetworkAndShape); } @@ -405,11 +405,11 @@ static Set readDemandInformation(Path csvLocationDeman * * @param scenario * @param demandInformation - * @param polygonsInShape + * @param indexShape * @param shapeCategory */ static void checkNewDemand(Scenario scenario, Set demandInformation, - Collection polygonsInShape, String shapeCategory) { + ShpOptions.Index indexShape, String shapeCategory) { for (DemandInformationElement newDemand : demandInformation) { Carriers carriers = (Carriers) scenario.getScenarioElement("carriers"); @@ -440,13 +440,13 @@ static void checkNewDemand(Scenario scenario, Set dema throw new RuntimeException("For the carrier " + newDemand.getCarrierName() + ": Select either share of population or number of locations"); if (newDemand.getAreasFirstJobElement() != null) { - if (polygonsInShape == null) + if (indexShape == null) throw new RuntimeException("You selected a certain area for the carrier" + newDemand.getCarrierName() + " although no shape file is loaded."); for (String demandArea : newDemand.getAreasFirstJobElement()) { boolean isInShape = false; - for (SimpleFeature singlePolygon : polygonsInShape) + for (SimpleFeature singlePolygon : indexShape.getAllFeatures()) if ((singlePolygon.getAttribute(shapeCategory) != null && singlePolygon.getAttribute(shapeCategory).equals(demandArea))) { isInShape = true; @@ -502,12 +502,12 @@ static void checkNewDemand(Scenario scenario, Set dema throw new RuntimeException("For the carrier " + newDemand.getCarrierName() + ": The percentage of the population should be more than 0 and maximum 100pct. Please check!"); if (newDemand.getAreasSecondJobElement() != null) { - if (polygonsInShape == null) + if (indexShape == null) throw new RuntimeException("You selected a certain area for the carrier" + newDemand.getCarrierName() + " although no shape file is loaded."); for (String demand : newDemand.getAreasSecondJobElement()) { boolean isInShape = false; - for (SimpleFeature singlePolygon : polygonsInShape) + for (SimpleFeature singlePolygon : indexShape.getAllFeatures()) if (singlePolygon.getAttribute(shapeCategory).equals(demand)) { isInShape = true; break; @@ -538,22 +538,22 @@ static void checkNewDemand(Scenario scenario, Set dema * Creates for every demand information the services/shipments for the carriers * * @param scenario - * @param polygonsInShape + * @param indexShape * @param demandInformation * @param population * @param combineSimilarJobs * @param crsTransformationNetworkAndShape */ - static void createDemandForCarriers(Scenario scenario, Collection polygonsInShape, + static void createDemandForCarriers(Scenario scenario, ShpOptions.Index indexShape, Set demandInformation, Population population, boolean combineSimilarJobs, CoordinateTransformation crsTransformationNetworkAndShape) { for (DemandInformationElement newDemandInformationElement : demandInformation) { if (newDemandInformationElement.getTypeOfDemand().equals("service")) - createServices(scenario, newDemandInformationElement, polygonsInShape, population, combineSimilarJobs, + createServices(scenario, newDemandInformationElement, indexShape, population, combineSimilarJobs, crsTransformationNetworkAndShape); else if (newDemandInformationElement.getTypeOfDemand().equals("shipment")) - createShipments(scenario, newDemandInformationElement, polygonsInShape, population, combineSimilarJobs, + createShipments(scenario, newDemandInformationElement, indexShape, population, combineSimilarJobs, crsTransformationNetworkAndShape); } @@ -564,14 +564,14 @@ else if (newDemandInformationElement.getTypeOfDemand().equals("shipment")) * * @param scenario * @param newDemandInformationElement - * @param polygonsInShape + * @param indexShape * @param population * @param combineSimilarJobs * @param crsTransformationNetworkAndShape */ private static void createServices(Scenario scenario, DemandInformationElement newDemandInformationElement, - Collection polygonsInShape, Population population, boolean combineSimilarJobs, - CoordinateTransformation crsTransformationNetworkAndShape) { + ShpOptions.Index indexShape, Population population, boolean combineSimilarJobs, + CoordinateTransformation crsTransformationNetworkAndShape) { int countOfLinks = 1; int distributedDemand = 0; @@ -599,7 +599,7 @@ else if (population == null) String samplingOption = String.valueOf(population.getAttributes().getAttribute("samplingOption")); if (areasForServiceLocations != null) - possiblePersonsForService = findPossiblePersons(population, areasForServiceLocations, polygonsInShape, + possiblePersonsForService = findPossiblePersons(population, areasForServiceLocations, indexShape, crsTransformationNetworkAndShape); else possiblePersonsForService.putAll(population.getPersons()); @@ -620,7 +620,7 @@ else if (samplingOption.equals("changeDemandOnLocation")) { numberOfServiceLocations = numberPossibleServices; } // find possible links for the services - HashMap, Link> possibleLinksForService = findAllPossibleLinks(scenario, polygonsInShape, + HashMap, Link> possibleLinksForService = findAllPossibleLinks(scenario, indexShape, crsTransformationNetworkAndShape, numberOfServiceLocations, areasForServiceLocations, locationsOfServices, possiblePersonsForService, nearestLinkPerPerson); @@ -635,7 +635,7 @@ else if (samplingOption.equals("changeDemandOnLocation")) { if (possibleLinksForService.size() > demandToDistribute) { for (int i = 0; i < demandToDistribute; i++) { - Link link = findNextUsedLink(scenario, polygonsInShape, possibleLinksForService, numberOfJobs, + Link link = findNextUsedLink(scenario, indexShape, possibleLinksForService, numberOfJobs, areasForServiceLocations, locationsOfServices, usedServiceLocations, possiblePersonsForService, nearestLinkPerPerson, crsTransformationNetworkAndShape, i); double serviceTime = newDemandInformationElement.getFirstJobElementTimePerUnit(); @@ -707,7 +707,7 @@ else if (samplingOption.equals("changeDemandOnLocation")) { if (locationsOfServices != null && locationsOfServices.length > i) { link = scenario.getNetwork().getLinks().get(Id.createLinkId(locationsOfServices[i])); } else - link = findNextUsedLink(scenario, polygonsInShape, possibleLinksForService, + link = findNextUsedLink(scenario, indexShape, possibleLinksForService, numberOfServiceLocations, areasForServiceLocations, locationsOfServices, usedServiceLocations, possiblePersonsForService, nearestLinkPerPerson, crsTransformationNetworkAndShape, i); @@ -756,14 +756,14 @@ else if (samplingOption.equals("changeDemandOnLocation")) { * * @param scenario * @param newDemandInformationElement - * @param polygonsInShape + * @param indexShape * @param population * @param combineSimilarJobs * @param crsTransformationNetworkAndShape */ private static void createShipments(Scenario scenario, DemandInformationElement newDemandInformationElement, - Collection polygonsInShape, Population population, boolean combineSimilarJobs, - CoordinateTransformation crsTransformationNetworkAndShape) { + ShpOptions.Index indexShape, Population population, boolean combineSimilarJobs, + CoordinateTransformation crsTransformationNetworkAndShape) { int countOfLinks = 1; int distributedDemand = 0; @@ -798,12 +798,12 @@ else if (population == null) String samplingOption = String.valueOf(population.getAttributes().getAttribute("samplingOption")); if (areasForPickupLocations != null) - possiblePersonsPickup = findPossiblePersons(population, areasForPickupLocations, polygonsInShape, + possiblePersonsPickup = findPossiblePersons(population, areasForPickupLocations, indexShape, crsTransformationNetworkAndShape); else possiblePersonsPickup.putAll(population.getPersons()); if (areasForDeliveryLocations != null) - possiblePersonsDelivery = findPossiblePersons(population, areasForDeliveryLocations, polygonsInShape, + possiblePersonsDelivery = findPossiblePersons(population, areasForDeliveryLocations, indexShape, crsTransformationNetworkAndShape); else possiblePersonsDelivery.putAll(population.getPersons()); @@ -862,10 +862,10 @@ else if (population == null) numberOfDeliveryLocations = numberPossibleJobsDelivery; } // find possible Links for delivery and pickup - HashMap, Link> possibleLinksPickup = findAllPossibleLinks(scenario, polygonsInShape, + HashMap, Link> possibleLinksPickup = findAllPossibleLinks(scenario, indexShape, crsTransformationNetworkAndShape, numberOfPickupLocations, areasForPickupLocations, setLocationsOfPickup, possiblePersonsPickup, nearestLinkPerPersonPickup); - HashMap, Link> possibleLinksDelivery = findAllPossibleLinks(scenario, polygonsInShape, + HashMap, Link> possibleLinksDelivery = findAllPossibleLinks(scenario, indexShape, crsTransformationNetworkAndShape, numberOfDeliveryLocations, areasForDeliveryLocations, setLocationsOfDelivery, possiblePersonsDelivery, nearestLinkPerPersonPickup); @@ -895,10 +895,10 @@ else if (population == null) for (int i = 0; i < demandToDistribute; i++) { Link linkPickup; Link linkDelivery; - linkPickup = findNextUsedLink(scenario, polygonsInShape, possibleLinksPickup, + linkPickup = findNextUsedLink(scenario, indexShape, possibleLinksPickup, numberOfPickupLocations, areasForPickupLocations, setLocationsOfPickup, usedPickupLocations, possiblePersonsPickup, nearestLinkPerPersonPickup, crsTransformationNetworkAndShape, i); - linkDelivery = findNextUsedLink(scenario, polygonsInShape, possibleLinksDelivery, + linkDelivery = findNextUsedLink(scenario, indexShape, possibleLinksDelivery, numberOfDeliveryLocations, areasForDeliveryLocations, setLocationsOfDelivery, usedDeliveryLocations, possiblePersonsDelivery, nearestLinkPerPersonDelivery, crsTransformationNetworkAndShape, i); @@ -971,12 +971,12 @@ else if (numberOfPickupLocations != null) { } if (pickupIsDemandBase) { linkPickup = demandBasedLink; - linkDelivery = findNextUsedLink(scenario, polygonsInShape, possibleLinksDelivery, + linkDelivery = findNextUsedLink(scenario, indexShape, possibleLinksDelivery, numberOfDeliveryLocations, areasForDeliveryLocations, setLocationsOfDelivery, usedDeliveryLocations, possiblePersonsDelivery, nearestLinkPerPersonDelivery, crsTransformationNetworkAndShape, countOfLinks - 1); while (usedDeliveryLocations.contains(linkDelivery.getId().toString())) { - linkDelivery = findNextUsedLink(scenario, polygonsInShape, possibleLinksDelivery, + linkDelivery = findNextUsedLink(scenario, indexShape, possibleLinksDelivery, numberOfDeliveryLocations, areasForDeliveryLocations, setLocationsOfDelivery, usedDeliveryLocations, possiblePersonsDelivery, nearestLinkPerPersonDelivery, crsTransformationNetworkAndShape, countOfLinks - 1); @@ -987,12 +987,12 @@ else if (numberOfPickupLocations != null) { } } else { linkDelivery = demandBasedLink; - linkPickup = findNextUsedLink(scenario, polygonsInShape, possibleLinksPickup, + linkPickup = findNextUsedLink(scenario, indexShape, possibleLinksPickup, numberOfPickupLocations, areasForPickupLocations, setLocationsOfPickup, usedPickupLocations, possiblePersonsPickup, nearestLinkPerPersonPickup, crsTransformationNetworkAndShape, countOfLinks - 1); while (usedPickupLocations.contains(linkPickup.getId().toString())) { - linkPickup = findNextUsedLink(scenario, polygonsInShape, possibleLinksPickup, + linkPickup = findNextUsedLink(scenario, indexShape, possibleLinksPickup, numberOfPickupLocations, areasForPickupLocations, setLocationsOfPickup, usedPickupLocations, possiblePersonsPickup, nearestLinkPerPersonPickup, crsTransformationNetworkAndShape, countOfLinks - 1); @@ -1037,10 +1037,10 @@ else if (numberOfPickupLocations != null) { if (demandToDistribute != 0 && demandToDistribute < numberOfJobs) throw new RuntimeException( "The resulting number of jobs is not feasible, because the demand is smaller then the number of jobs. Please check!"); - Link linkPickup = findNextUsedLink(scenario, polygonsInShape, possibleLinksPickup, + Link linkPickup = findNextUsedLink(scenario, indexShape, possibleLinksPickup, numberOfPickupLocations, areasForPickupLocations, setLocationsOfPickup, usedPickupLocations, possiblePersonsPickup, nearestLinkPerPersonPickup, crsTransformationNetworkAndShape, i); - Link linkDelivery = findNextUsedLink(scenario, polygonsInShape, possibleLinksDelivery, + Link linkDelivery = findNextUsedLink(scenario, indexShape, possibleLinksDelivery, numberOfDeliveryLocations, areasForDeliveryLocations, setLocationsOfDelivery, usedDeliveryLocations, possiblePersonsDelivery, nearestLinkPerPersonDelivery, crsTransformationNetworkAndShape, i); @@ -1241,7 +1241,7 @@ private static void reduceNumberOfJobsIfSameCharacteristics(Scenario scenario, * Finds and returns all possible links for this job. * * @param scenario - * @param polygonsInShape + * @param indexShape * @param crsTransformationNetworkAndShape * @param numberOfLocations * @param areasForLocations @@ -1251,22 +1251,24 @@ private static void reduceNumberOfJobsIfSameCharacteristics(Scenario scenario, * @return */ private static HashMap, Link> findAllPossibleLinks(Scenario scenario, - Collection polygonsInShape, CoordinateTransformation crsTransformationNetworkAndShape, - Integer numberOfLocations, String[] areasForLocations, String[] setLocations, - HashMap, Person> possiblePersons, - HashMap, HashMap> nearestLinkPerPerson) { + ShpOptions.Index indexShape, CoordinateTransformation crsTransformationNetworkAndShape, + Integer numberOfLocations, String[] areasForLocations, String[] setLocations, + HashMap, Person> possiblePersons, + HashMap, HashMap> nearestLinkPerPerson) { HashMap, Link> possibleLinks = new HashMap, Link>(); if (numberOfLocations == null) { for (Link link : scenario.getNetwork().getLinks().values()) - if (!link.getId().toString().contains("pt") && (!link.getAttributes().getAsMap().containsKey("type") || !link.getAttributes().getAsMap().get("type").toString().contains("motorway")) && FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, - areasForLocations, crsTransformationNetworkAndShape)) { + if (!link.getId().toString().contains("pt") && (!link.getAttributes().getAsMap().containsKey( + "type") || !link.getAttributes().getAsMap().get("type").toString().contains( + "motorway")) && FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, + areasForLocations, crsTransformationNetworkAndShape)) { possibleLinks.put(link.getId(), link); } } else { Link newPossibleLink; while (possibleLinks.size() < numberOfLocations) { newPossibleLink = findPossibleLinkForDemand(possibleLinks, possiblePersons, nearestLinkPerPerson, - polygonsInShape, areasForLocations, numberOfLocations, scenario, setLocations, + indexShape, areasForLocations, numberOfLocations, scenario, setLocations, crsTransformationNetworkAndShape); if (!possibleLinks.containsKey(newPossibleLink.getId())) possibleLinks.put(newPossibleLink.getId(), newPossibleLink); @@ -1280,7 +1282,7 @@ private static HashMap, Link> findAllPossibleLinks(Scenario scenario, * Finds the next link which can be used as a location. * * @param scenario - * @param polygonsInShape + * @param indexShape * @param possibleLinks * @param selectedNumberOfLocations * @param areasForLocations @@ -1292,7 +1294,7 @@ private static HashMap, Link> findAllPossibleLinks(Scenario scenario, * @param i * @return */ - private static Link findNextUsedLink(Scenario scenario, Collection polygonsInShape, + private static Link findNextUsedLink(Scenario scenario, ShpOptions.Index indexShape, HashMap, Link> possibleLinks, Integer selectedNumberOfLocations, String[] areasForLocations, String[] selectedLocations, ArrayList usedLocations, HashMap, Person> possiblePersons, HashMap, HashMap> nearestLinkPerPerson, @@ -1306,7 +1308,7 @@ private static Link findNextUsedLink(Scenario scenario, Collection usedLocations.size() && usedLocations.contains(link.getId().toString()))) link = findPossibleLinkForDemand(possibleLinks, possiblePersons, nearestLinkPerPerson, - polygonsInShape, areasForLocations, selectedNumberOfLocations, scenario, selectedLocations, + indexShape, areasForLocations, selectedNumberOfLocations, scenario, selectedLocations, crsTransformationNetworkAndShape); } else { link = scenario.getNetwork().getLinks() @@ -1320,12 +1322,12 @@ private static Link findNextUsedLink(Scenario scenario, Collection, Person> findPossiblePersons(Population population, - String[] areasForServiceLocations, Collection polygonsInShape, + String[] areasForServiceLocations, ShpOptions.Index indexShape, CoordinateTransformation crsTransformationNetworkAndShape) { HashMap, Person> possiblePersons = new HashMap, Person>(); @@ -1335,7 +1337,7 @@ private static HashMap, Person> findPossiblePersons(Population popula if (crsTransformationNetworkAndShape != null) coord = crsTransformationNetworkAndShape.transform(coord); - if (FreightDemandGenerationUtils.checkPositionInShape(null, MGC.coord2Point(coord), polygonsInShape, + if (FreightDemandGenerationUtils.checkPositionInShape(null, coord, indexShape, areasForServiceLocations, crsTransformationNetworkAndShape)) possiblePersons.put(person.getId(), person); } @@ -1397,7 +1399,7 @@ private static Coord getHomeCoord(Person person) { * @param possibleLinks * @param possiblePersons * @param nearestLinkPerPerson - * @param polygonsInShape + * @param indexShape * @param areasForTheDemand * @param selectedNumberOfLocations * @param scenario @@ -1406,10 +1408,10 @@ private static Coord getHomeCoord(Person person) { * @return */ private static Link findPossibleLinkForDemand(HashMap, Link> possibleLinks, - HashMap, Person> possiblePersons, - HashMap, HashMap> nearestLinkPerPerson, - Collection polygonsInShape, String[] areasForTheDemand, Integer selectedNumberOfLocations, - Scenario scenario, String[] selectedLocations, CoordinateTransformation crsTransformationNetworkAndShape) { + HashMap, Person> possiblePersons, + HashMap, HashMap> nearestLinkPerPerson, + ShpOptions.Index indexShape, String[] areasForTheDemand, Integer selectedNumberOfLocations, + Scenario scenario, String[] selectedLocations, CoordinateTransformation crsTransformationNetworkAndShape) { Link selectedlink = null; Link newLink = null; if (selectedNumberOfLocations == null) @@ -1440,8 +1442,8 @@ private static Link findPossibleLinkForDemand(HashMap, Link> possibleLi if (!newLink.getId().toString().contains("pt") && (!newLink.getAttributes().getAsMap().containsKey("type") || !newLink.getAttributes().getAsMap().get("type").toString().contains("motorway")) - && (polygonsInShape == null || FreightDemandGenerationUtils.checkPositionInShape(newLink, null, - polygonsInShape, areasForTheDemand, crsTransformationNetworkAndShape))) + && (indexShape == null || FreightDemandGenerationUtils.checkPositionInShape(newLink, null, + indexShape, areasForTheDemand, crsTransformationNetworkAndShape))) selectedlink = newLink; } return selectedlink; diff --git a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGeneration.java b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGeneration.java index 20e12ac8c92..d43ee64522e 100644 --- a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGeneration.java +++ b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGeneration.java @@ -40,13 +40,11 @@ import org.matsim.freight.carriers.controler.CarrierModule; import org.matsim.freight.carriers.controler.CarrierScoringFunctionFactory; import org.matsim.freight.carriers.usecases.chessboard.CarrierScoringFunctionFactoryImpl; -import org.opengis.feature.simple.SimpleFeature; import picocli.CommandLine; import javax.management.InvalidAttributeValueException; import java.io.IOException; import java.nio.file.Path; -import java.util.Collection; import java.util.Objects; import java.util.concurrent.ExecutionException; @@ -187,20 +185,20 @@ public Integer call() throws IOException, InvalidAttributeValueException, Execut // load or create carrier Scenario scenario = ScenarioUtils.loadScenario(config); - Collection polygonsInShape = null; + ShpOptions.Index indexShape = null; shp = new ShpOptions(shapeFilePath, shapeCRS, null); if (shp.isDefined()) { log.warn("Use of shpFile. Locations for the carriers and the demand only in shp: " + shp.getShapeFile()); - polygonsInShape = shp.readFeatures(); + indexShape = shp.createIndex(shapeCategory); crsTransformationFromNetworkToShape = shp.createTransformation(networkCRS); } log.info("Start creating carriers. Selected option: " + selectedCarrierInputOption); - createCarrier(scenario, selectedCarrierInputOption, csvCarrierPath, polygonsInShape, + createCarrier(scenario, selectedCarrierInputOption, csvCarrierPath, indexShape, defaultJspritIterations, crsTransformationFromNetworkToShape); // create the demand log.info("Start creating the demand. Selected option: " + selectedCarrierInputOption); - createDemand(selectedDemandGenerationOption, scenario, csvDemandPath, polygonsInShape, populationFilePath, + createDemand(selectedDemandGenerationOption, scenario, csvDemandPath, indexShape, populationFilePath, selectedPopulationSamplingOption, selectedPopulationOption, Boolean.getBoolean(combineSimilarJobs), crsTransformationFromNetworkToShape); @@ -291,13 +289,13 @@ private static void prepareVehicles(Config config, String vehicleTypesFileLocati * @param scenario * @param selectedCarrierInputOption * @param csvLocationCarrier - * @param polygonsInShape + * @param indexShape * @param defaultJspritIterations * @param crsTransformationNetworkAndShape * @throws IOException */ private void createCarrier(Scenario scenario, CarrierInputOptions selectedCarrierInputOption, - Path csvLocationCarrier, Collection polygonsInShape, + Path csvLocationCarrier, ShpOptions.Index indexShape, int defaultJspritIterations, CoordinateTransformation crsTransformationNetworkAndShape) throws IOException { FreightCarriersConfigGroup freightCarriersConfigGroup = ConfigUtils.addOrGetModule(scenario.getConfig(), @@ -312,7 +310,7 @@ private void createCarrier(Scenario scenario, CarrierInputOptions selectedCarrie CarriersUtils.loadCarriersAccordingToFreightConfig(scenario); log.info("Load carriers from: " + freightCarriersConfigGroup.getCarriersFile()); CarrierReaderFromCSV.readAndCreateCarrierFromCSV(scenario, freightCarriersConfigGroup, csvLocationCarrier, - polygonsInShape, defaultJspritIterations, crsTransformationNetworkAndShape, shapeCategory); + indexShape, defaultJspritIterations, crsTransformationNetworkAndShape, shapeCategory); } } case readCarrierFile -> { @@ -327,7 +325,7 @@ private void createCarrier(Scenario scenario, CarrierInputOptions selectedCarrie case createCarriersFromCSV -> // creates all carriers based on the given information in the read carrier csv CarrierReaderFromCSV.readAndCreateCarrierFromCSV(scenario, freightCarriersConfigGroup, csvLocationCarrier, - polygonsInShape, defaultJspritIterations, crsTransformationNetworkAndShape, shapeCategory); + indexShape, defaultJspritIterations, crsTransformationNetworkAndShape, shapeCategory); default -> throw new RuntimeException("no method to create or read carrier selected."); } } @@ -338,7 +336,7 @@ private void createCarrier(Scenario scenario, CarrierInputOptions selectedCarrie * @param selectedDemandGenerationOption * @param scenario * @param csvLocationDemand - * @param polygonsInShape + * @param indexShape * @param populationFilePath * @param selectedSamplingOption * @param selectedPopulationOption @@ -347,14 +345,14 @@ private void createCarrier(Scenario scenario, CarrierInputOptions selectedCarrie * @throws IOException */ private void createDemand(DemandGenerationOptions selectedDemandGenerationOption, Scenario scenario, - Path csvLocationDemand, Collection polygonsInShape, String populationFilePath, - PopulationSamplingOption selectedSamplingOption, PopulationOptions selectedPopulationOption, - boolean combineSimilarJobs, CoordinateTransformation crsTransformationNetworkAndShape) throws IOException { + Path csvLocationDemand, ShpOptions.Index indexShape, String populationFilePath, + PopulationSamplingOption selectedSamplingOption, PopulationOptions selectedPopulationOption, + boolean combineSimilarJobs, CoordinateTransformation crsTransformationNetworkAndShape) throws IOException { switch (selectedDemandGenerationOption) { case createDemandFromCSV -> // creates the demand by using the information given in the read csv file - DemandReaderFromCSV.readAndCreateDemand(scenario, csvLocationDemand, polygonsInShape, combineSimilarJobs, + DemandReaderFromCSV.readAndCreateDemand(scenario, csvLocationDemand, indexShape, combineSimilarJobs, crsTransformationNetworkAndShape, null, shapeCategory); case createDemandFromCSVAndUsePopulation -> { /* @@ -390,14 +388,14 @@ private void createDemand(DemandGenerationOptions selectedDemandGenerationOption break; case useHolePopulation: // uses the hole population as possible demand locations - DemandReaderFromCSV.readAndCreateDemand(scenario, csvLocationDemand, polygonsInShape, + DemandReaderFromCSV.readAndCreateDemand(scenario, csvLocationDemand, indexShape, combineSimilarJobs, crsTransformationNetworkAndShape, population, shapeCategory); break; case usePopulationInShape: // uses only the population with home location in the given shape file FreightDemandGenerationUtils.reducePopulationToShapeArea(population, shp.createIndex(populationCRS, "_")); - DemandReaderFromCSV.readAndCreateDemand(scenario, csvLocationDemand, polygonsInShape, + DemandReaderFromCSV.readAndCreateDemand(scenario, csvLocationDemand, indexShape, combineSimilarJobs, crsTransformationNetworkAndShape, population, shapeCategory); break; default: diff --git a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java index b2d70951be8..c85382575d9 100644 --- a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java +++ b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java @@ -21,29 +21,30 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.locationtech.jts.geom.Geometry; -import org.locationtech.jts.geom.Point; import org.matsim.api.core.v01.Coord; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.network.Network; -import org.matsim.api.core.v01.population.*; +import org.matsim.api.core.v01.population.Person; +import org.matsim.api.core.v01.population.Plan; +import org.matsim.api.core.v01.population.Population; import org.matsim.application.options.ShpOptions; +import org.matsim.core.controler.Controler; +import org.matsim.core.population.PopulationUtils; +import org.matsim.core.router.TripStructureUtils; +import org.matsim.core.utils.geometry.CoordinateTransformation; +import org.matsim.core.utils.geometry.geotools.MGC; import org.matsim.freight.carriers.Carrier; import org.matsim.freight.carriers.CarrierService; import org.matsim.freight.carriers.CarrierShipment; import org.matsim.freight.carriers.CarriersUtils; -import org.matsim.core.controler.Controler; -import org.matsim.core.utils.geometry.CoordinateTransformation; -import org.matsim.core.utils.geometry.geotools.MGC; -import org.opengis.feature.simple.SimpleFeature; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; import java.util.List; +import java.util.Objects; /** * Collection of different methods for the FreightDemandGeneration. @@ -164,42 +165,34 @@ static void reducePopulationToShapeArea(Population population, ShpOptions.Index * Checks if a link is one of the possible areas. * * @param link - * @param point - * @param polygonsInShape + * @param givenCoord + * @param indexShape * @param possibleAreas * @param crsTransformationNetworkAndShape * @return */ - static boolean checkPositionInShape(Link link, Point point, Collection polygonsInShape, - String[] possibleAreas, CoordinateTransformation crsTransformationNetworkAndShape) { - - if (polygonsInShape == null) + static boolean checkPositionInShape(Link link, Coord givenCoord, ShpOptions.Index indexShape, + String[] possibleAreas, CoordinateTransformation crsTransformationNetworkAndShape) { + if (indexShape == null) return true; - boolean isInShape = false; - Point p = null; - if (link != null && point == null) { + Coord coordToCheck = null; + if (link != null && givenCoord == null) { if (crsTransformationNetworkAndShape != null) - p = MGC.coord2Point(crsTransformationNetworkAndShape.transform(getCoordOfMiddlePointOfLink(link))); + coordToCheck = crsTransformationNetworkAndShape.transform(getCoordOfMiddlePointOfLink(link)); else - p = MGC.coord2Point(getCoordOfMiddlePointOfLink(link)); - } else if (link == null && point != null) - p = point; - for (SimpleFeature singlePolygon : polygonsInShape) { - if (possibleAreas != null) { - for (String area : possibleAreas) { - if (area.equals(singlePolygon.getAttribute("Ortsteil")) - || area.equals(singlePolygon.getAttribute("BEZNAME"))) - if (((Geometry) singlePolygon.getDefaultGeometry()).contains(p)) { - return true; - } - } - } else { - if (((Geometry) singlePolygon.getDefaultGeometry()).contains(p)) { + coordToCheck = getCoordOfMiddlePointOfLink(link); + } else if (link == null && givenCoord != null) + coordToCheck = givenCoord; + if (possibleAreas != null) { + for (String area : possibleAreas) { + if (Objects.equals(indexShape.query(coordToCheck), area)) { return true; } } + } else { + return indexShape.contains(coordToCheck); } - return isInShape; + return false; } /** diff --git a/contribs/application/src/test/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSVTest.java b/contribs/application/src/test/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSVTest.java index 1bcb22e6b80..96e405eefd3 100644 --- a/contribs/application/src/test/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSVTest.java +++ b/contribs/application/src/test/java/org/matsim/freightDemandGeneration/CarrierReaderFromCSVTest.java @@ -1,14 +1,7 @@ package org.matsim.freightDemandGeneration; -import java.io.IOException; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -17,21 +10,21 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.network.Network; import org.matsim.application.options.ShpOptions; -import org.matsim.freight.carriers.FreightCarriersConfigGroup; -import org.matsim.freight.carriers.Carrier; -import org.matsim.freight.carriers.CarrierCapabilities.FleetSize; -import org.matsim.freight.carriers.CarriersUtils; -import org.matsim.freight.carriers.CarrierVehicle; import org.matsim.core.config.Config; import org.matsim.core.config.ConfigUtils; import org.matsim.core.network.NetworkUtils; import org.matsim.core.scenario.ScenarioUtils; +import org.matsim.freight.carriers.Carrier; +import org.matsim.freight.carriers.CarrierCapabilities.FleetSize; +import org.matsim.freight.carriers.CarrierVehicle; +import org.matsim.freight.carriers.CarriersUtils; +import org.matsim.freight.carriers.FreightCarriersConfigGroup; import org.matsim.freightDemandGeneration.CarrierReaderFromCSV.CarrierInformationElement; import org.matsim.testcases.MatsimTestUtils; -import org.opengis.feature.simple.SimpleFeature; -import it.unimi.dsi.fastutil.objects.Object2IntMap; -import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import java.io.IOException; +import java.nio.file.Path; +import java.util.*; /** * @author Ricardo Ewert @@ -54,13 +47,13 @@ void carrierCreation() throws IOException { Path carrierCSVLocation = Path.of(utils.getPackageInputDirectory() + "testCarrierCSV.csv"); Path shapeFilePath = Path.of(utils.getPackageInputDirectory() + "testShape/testShape.shp"); ShpOptions shp = new ShpOptions(shapeFilePath, "WGS84", null); - Collection polygonsInShape = shp.readFeatures(); Set allNewCarrierInformation = CarrierReaderFromCSV .readCarrierInformation(carrierCSVLocation); String shapeCategory = "Ortsteil"; - CarrierReaderFromCSV.checkNewCarrier(allNewCarrierInformation, freightCarriersConfigGroup, scenario, polygonsInShape, shapeCategory); + ShpOptions.Index indexShape = shp.createIndex(shapeCategory); + CarrierReaderFromCSV.checkNewCarrier(allNewCarrierInformation, freightCarriersConfigGroup, scenario, indexShape, shapeCategory); CarrierReaderFromCSV.createNewCarrierAndAddVehicleTypes(scenario, allNewCarrierInformation, freightCarriersConfigGroup, - polygonsInShape, 1, null); + indexShape, 1, null); Assertions.assertEquals(3, CarriersUtils.getCarriers(scenario).getCarriers().size()); Assertions.assertTrue( CarriersUtils.getCarriers(scenario).getCarriers().containsKey(Id.create("testCarrier1", Carrier.class))); @@ -146,9 +139,9 @@ void carrierCreation() throws IOException { if (!depot.equals("j(2,6)R")) { Link link = network.getLinks().get(Id.createLinkId(depot)); Assertions.assertTrue( - FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, null, null)); - Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, new String[]{"area1"}, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, new String[]{"area2"}, null)); + FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, null, null)); + Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[]{"area1"}, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[]{"area2"}, null)); } } diff --git a/contribs/application/src/test/java/org/matsim/freightDemandGeneration/DemandReaderFromCSVTest.java b/contribs/application/src/test/java/org/matsim/freightDemandGeneration/DemandReaderFromCSVTest.java index fcffb7efe40..c9133b6b56b 100644 --- a/contribs/application/src/test/java/org/matsim/freightDemandGeneration/DemandReaderFromCSVTest.java +++ b/contribs/application/src/test/java/org/matsim/freightDemandGeneration/DemandReaderFromCSVTest.java @@ -78,9 +78,10 @@ void demandCreation() throws IOException { freightCarriersConfigGroup.setCarriersVehicleTypesFile(utils.getPackageInputDirectory() + "testVehicleTypes.xml"); Path carrierCSVLocation = Path.of(utils.getPackageInputDirectory() + "testCarrierCSV.csv"); Path demandCSVLocation = Path.of(utils.getPackageInputDirectory() + "testDemandCSV.csv"); - String shapeCategory = "Ortsteil"; Path shapeFilePath = Path.of(utils.getPackageInputDirectory() + "testShape/testShape.shp"); ShpOptions shp = new ShpOptions(shapeFilePath, "WGS84", null); + String shapeCategory = "Ortsteil"; + ShpOptions.Index indexShape = shp.createIndex("Ortsteil"); Collection polygonsInShape = shp.readFeatures(); String populationLocation = utils.getPackageInputDirectory() + "testPopulation.xml"; Population population = PopulationUtils.readPopulation(populationLocation); @@ -90,10 +91,10 @@ void demandCreation() throws IOException { Set allNewCarrierInformation = CarrierReaderFromCSV .readCarrierInformation(carrierCSVLocation); CarrierReaderFromCSV.createNewCarrierAndAddVehicleTypes(scenario, allNewCarrierInformation, freightCarriersConfigGroup, - polygonsInShape, 1, null); + indexShape, 1, null); Set demandInformation = DemandReaderFromCSV.readDemandInformation(demandCSVLocation); - DemandReaderFromCSV.checkNewDemand(scenario, demandInformation, polygonsInShape, shapeCategory); - DemandReaderFromCSV.createDemandForCarriers(scenario, polygonsInShape, demandInformation, population, false, + DemandReaderFromCSV.checkNewDemand(scenario, demandInformation, indexShape, shapeCategory); + DemandReaderFromCSV.createDemandForCarriers(scenario, indexShape, demandInformation, population, false, null); Assertions.assertEquals(3, CarriersUtils.getCarriers(scenario).getCarriers().size()); Assertions.assertTrue( @@ -142,10 +143,10 @@ void demandCreation() throws IOException { for (String locationsOfServiceElement : locationsPerServiceElement.get("serviceElement1")) { Link link = network.getLinks().get(Id.createLinkId(locationsOfServiceElement)); Assertions.assertTrue( - FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, null, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, + FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, null, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[] { "area1" }, null)); - Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, + Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[] { "area2" }, null)); } Assertions.assertEquals(4, locationsPerServiceElement.get("serviceElement2").size()); @@ -230,10 +231,10 @@ void demandCreation() throws IOException { for (String locationsOfShipmentElement : locationsPerShipmentElement.get("ShipmenElement1_delivery")) { Link link = network.getLinks().get(Id.createLinkId(locationsOfShipmentElement)); Assertions.assertTrue( - FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, null, null)); - Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, + FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, null, null)); + Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[] { "area1" }, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[] { "area2" }, null)); } } diff --git a/contribs/application/src/test/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtilsTest.java b/contribs/application/src/test/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtilsTest.java index e1528caf871..4015178449d 100644 --- a/contribs/application/src/test/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtilsTest.java +++ b/contribs/application/src/test/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtilsTest.java @@ -1,12 +1,8 @@ package org.matsim.freightDemandGeneration; -import java.nio.file.Path; -import java.util.Collection; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; -import org.locationtech.jts.geom.Point; import org.matsim.api.core.v01.Coord; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.network.Link; @@ -16,10 +12,10 @@ import org.matsim.application.options.ShpOptions; import org.matsim.core.network.NetworkUtils; import org.matsim.core.population.PopulationUtils; -import org.matsim.core.utils.geometry.geotools.MGC; import org.matsim.testcases.MatsimTestUtils; -import org.opengis.feature.simple.SimpleFeature; - + +import java.nio.file.Path; + /** * @author Ricardo Ewert * @@ -27,9 +23,9 @@ public class FreightDemandGenerationUtilsTest { @RegisterExtension - private MatsimTestUtils utils = new MatsimTestUtils(); - - @Test + private MatsimTestUtils utils = new MatsimTestUtils(); + + @Test void testPreparePopulation() { String populationLocation = utils.getPackageInputDirectory() + "testPopulation.xml"; Population population = PopulationUtils.readPopulation(populationLocation); @@ -78,9 +74,9 @@ void testPreparePopulation() { Assertions.assertEquals(5200.0,person.getAttributes().getAttribute("homeY")); Assertions.assertEquals(0, person.getPlans().size()); Assertions.assertNull(person.getSelectedPlan()); - } - - @Test + } + + @Test void testCoordOfMiddlePointOfLink() { Network network = NetworkUtils.readNetwork("https://raw.githubusercontent.com/matsim-org/matsim-libs/master/examples/scenarios/freight-chessboard-9x9/grid9x9.xml"); Link link = network.getLinks().get(Id.createLinkId("i(8,8)")); @@ -91,9 +87,9 @@ void testCoordOfMiddlePointOfLink() { Coord middlePoint2 = FreightDemandGenerationUtils.getCoordOfMiddlePointOfLink(link2); Assertions.assertEquals(5000, middlePoint2.getX(), MatsimTestUtils.EPSILON); Assertions.assertEquals(7500, middlePoint2.getY(), MatsimTestUtils.EPSILON); - } - - @Test + } + + @Test void testReducePopulationToShapeArea() { String populationLocation = utils.getPackageInputDirectory() + "testPopulation.xml"; Population population = PopulationUtils.readPopulation(populationLocation); @@ -105,38 +101,38 @@ void testReducePopulationToShapeArea() { Assertions.assertEquals(6, population.getPersons().size()); Assertions.assertFalse(population.getPersons().containsKey(Id.createPersonId("person2"))); Assertions.assertFalse(population.getPersons().containsKey(Id.createPersonId("person4"))); - } - - @Test + } + + @Test void testCheckPositionInShape_link() { Network network = NetworkUtils.readNetwork("https://raw.githubusercontent.com/matsim-org/matsim-libs/master/examples/scenarios/freight-chessboard-9x9/grid9x9.xml"); Link link = network.getLinks().get(Id.createLinkId("i(8,8)")); Path shapeFilePath = Path.of(utils.getPackageInputDirectory() + "testShape/testShape.shp"); ShpOptions shp = new ShpOptions(shapeFilePath,"WGS84", null); - Collection polygonsInShape = shp.readFeatures(); - Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, null, null)); - Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, new String[]{"area1"}, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, new String[]{"area2"}, null)); + ShpOptions.Index indexShape = shp.createIndex("Ortsteil"); + Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, null, null)); + Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[]{"area1"}, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[]{"area2"}, null)); link = network.getLinks().get(Id.createLinkId("i(6,3)R")); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, null, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, new String[]{"area1"}, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, polygonsInShape, new String[]{"area2"}, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, null, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[]{"area1"}, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(link, null, indexShape, new String[]{"area2"}, null)); + + } - } - - @Test + @Test void testCheckPositionInShape_point() { Path shapeFilePath = Path.of(utils.getPackageInputDirectory() + "testShape/testShape.shp"); ShpOptions shp = new ShpOptions(shapeFilePath,"WGS84", null); - Collection polygonsInShape = shp.readFeatures(); - Point point = MGC.xy2Point(6000, 6000); - Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(null, point, polygonsInShape, null, null)); - Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(null, point, polygonsInShape, new String[]{"area1"}, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, point, polygonsInShape, new String[]{"area2"}, null)); - point = MGC.xy2Point(2000, 2000); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, point, polygonsInShape, null, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, point, polygonsInShape, new String[]{"area1"}, null)); - Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, point, polygonsInShape, new String[]{"area2"}, null)); + ShpOptions.Index indexShape = shp.createIndex("Ortsteil"); + Coord coord = new Coord(6000, 6000); + Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(null, coord, indexShape, null, null)); + Assertions.assertTrue(FreightDemandGenerationUtils.checkPositionInShape(null, coord, indexShape, new String[]{"area1"}, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, coord, indexShape, new String[]{"area2"}, null)); + coord = new Coord(2000, 2000); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, coord, indexShape, null, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, coord, indexShape, new String[]{"area1"}, null)); + Assertions.assertFalse(FreightDemandGenerationUtils.checkPositionInShape(null, coord, indexShape, new String[]{"area2"}, null)); } }