Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with RunCreateNetworkFromOSM.java #1170

Open
MarcinKinque opened this issue Oct 7, 2024 · 2 comments
Open

Problem with RunCreateNetworkFromOSM.java #1170

MarcinKinque opened this issue Oct 7, 2024 · 2 comments

Comments

@MarcinKinque
Copy link

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
Copy link
Member

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants