matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
82 stars 180 forks source link

Problem with RunCreateNetworkFromOSM.java #1170

Open MarcinKinque opened 1 week ago

MarcinKinque commented 1 week ago

I found the code example src/main/java/org/matsim/codeexamples/network/RunCreateNetworkFromOSM.java and inserted my file paths downloaded from Geofabrik.com

The code with the inserted paths:

package org.matsim.codeexamples.network;

import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.osm.networkReader.LinkProperties;
import org.matsim.contrib.osm.networkReader.OsmTags;
import org.matsim.contrib.osm.networkReader.SupersonicOsmNetworkReader;
import org.matsim.core.network.algorithms.NetworkCleaner;
import org.matsim.core.network.io.NetworkWriter;
import org.matsim.core.utils.geometry.CoordinateTransformation;
import org.matsim.core.utils.geometry.transformations.TransformationFactory;
import org.matsim.utils.gis.shp2matsim.ShpGeometryUtils;

import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Example on how to convert osm data from e.g. http://download.geofabrik.de into a MATSim network. This example puts all
 * motorways and primary roads into the MATSim network. If a link is contained in the supplied shape, also minor and
 * residential roads are put into the MATSim network.
 * <p>
 * After parsing the OSM-data, unreachable areas of the network are removed by using the network cleaner
 */
public class RunCreateNetworkShape {

    // UTM32n (EPSG:25832) is a common projection system for parts of Europe
    private static String UTM32nAsEpsg = "EPSG:25832";

    // Input paths for the OSM and Shapefile data
    private static Path input = Paths.get("D:\\intelliJ\\Matsim_example\\matsim-example-project\\Einfaches_Beispiel_kleines_Netz\\stuttgart-regbez-latest.osm.pbf");
    private static Path filterShape = Paths.get("D:\\intelliJ\\Matsim_example\\matsim-example-project\\Einfaches_Beispiel_kleines_Netz\\stuttgart-regbez-latest-free.shp");

    public static void main(String[] args) throws MalformedURLException {
        new RunCreateNetworkShape().create();
    }

    private void create() throws MalformedURLException {

        // Choose an appropriate coordinate transformation. OSM Data is in WGS84. EPSG:25832 or EPSG:25833 is commonly used in central Europe
        CoordinateTransformation transformation = TransformationFactory.getCoordinateTransformation(
                TransformationFactory.WGS84, UTM32nAsEpsg
        );

        // Load the geometries from the Shapefile, which will be used to filter the network creation process
        List<PreparedGeometry> filterGeometries = ShpGeometryUtils.loadPreparedGeometries(filterShape.toUri().toURL());

        // Create an OSM network reader and set the coordinate transformation and link filtering
        SupersonicOsmNetworkReader reader = new SupersonicOsmNetworkReader.Builder()
                .setCoordinateTransformation(transformation)
                .setIncludeLinkAtCoordWithHierarchy((coord, hierarchyLevel) -> {

                    // Include all links that are motorways, trunks, or primary-streets regardless of their location
                    if (hierarchyLevel <= LinkProperties.LEVEL_PRIMARY) return true;

                    // Within the shape, include all links that are contained in the OSM file
                    return ShpGeometryUtils.isCoordInPreparedGeometries(coord, filterGeometries);
                })
                .setAfterLinkCreated((link, osmTags, direction) -> {

                    // If the original OSM link contains a cycleway tag, add bicycle as allowed transport mode
                    if (osmTags.containsKey(OsmTags.CYCLEWAY)) {
                        Set<String> modes = new HashSet<>(link.getAllowedModes());
                        modes.add(TransportMode.bike);
                        link.setAllowedModes(modes);
                    }
                })
                .build();

        // Read the OSM data and create the MATSim network
        Network network = reader.read(input.toString());

        // Clean the network to remove unconnected components where agents might get stuck
        new NetworkCleaner().run(network);

        // Write the network to a file
        new NetworkWriter(network).write("D:\\intelliJ\\Matsim_example\\matsim-example-project\\plans_output\\STGTnetwork.xml.gz");
    }
}

when I run this this output appears:

2024-10-07T23:01:32,209  WARN MGC:179 Assuming that coordinates are in longitude first notation, i.e. (longitude, latitude).
2024-10-07T23:01:33,280  WARN MGC:179 Assuming that coordinates are in longitude first notation, i.e. (longitude, latitude).
2024-10-07T23:01:33,742  INFO GeoFileReader:106 will try to read from /D:/intelliJ/Matsim_example/matsim-example-project/Einfaches_Beispiel_kleines_Netz/stuttgart-regbez-latest-free.shp/
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.geotools.data.FileDataStore.getFeatureSource()" because "dataStore" is null
    at org.matsim.core.utils.gis.GeoFileReader.getSimpleFeatures(GeoFileReader.java:118)
    at org.matsim.core.utils.gis.GeoFileReader.getAllFeatures(GeoFileReader.java:107)
    at org.matsim.utils.gis.shp2matsim.ShpGeometryUtils.loadPreparedGeometries(ShpGeometryUtils.java:44)
    at org.matsim.codeexamples.network.RunCreateNetworkShape.create(RunCreateNetworkShape.java:50)
    at org.matsim.codeexamples.network.RunCreateNetworkShape.main(RunCreateNetworkShape.java:39)

Process finished with exit code 1

what is the problem with it the .shp is a folder with different files like .shp .prj .cpg... do I understand it false or is there something else I am missing?

I would appreaciate every help.

Thank you!

Janekdererste commented 1 week ago

Indeed, the program crashes when it tries to load your filter shape file. The shape file format consists of one main file ending with .shp and several other files which you already enumerated. You should make sure that the other files are in the same folder as the .shp file and have the same filename except for the extension.

what is the problem with it the .shp is a folder with different files like .shp .prj .cpg...

I am not quite sure, what exactly you are trying to say here. The exact meaning might have been lost in translation.

Janekdererste commented 1 week ago

Parsing the osm file usually takes a lot of ram. Make sure to assign sufficient ram by setting Xmx10G, or however much you can spare on your machine.