matsim-org / matsim-code-examples

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

Where to find code that converts from a network.xml to a shapefile? #99

Closed ClarissaLiv closed 5 years ago

ClarissaLiv commented 5 years ago

I would like to convert the output network of a MATSim run into a shapefile so that I can view it in QGIS. I cannot find the "Network Editor" referred to by this post: https://github.com/matsim-org/matsim-code-examples/wiki/faq-109448386

nor can I find a finished version of this code: https://sourceforge.net/p/matsim/mailman/message/33613820/

Perhaps someone could give me the keywords I need to find this bit of code?

Thank you.

Clarissa

ClarissaLiv commented 5 years ago

Partial update: The "Network Editor" referred to in this post https://github.com/matsim-org/matsim-code-examples/wiki/faq-109448386 seems to be the MATSim plugin for JOSM https://matsim.atlassian.net/wiki/spaces/MATPUB/pages/18317321/JOSM-Plugin . I am trying to use that, but....

I'd really rather use a script because I working with very large networks. I swear I found a great script this morning that was really similar to what is started in this thread: https://sourceforge.net/p/matsim/mailman/message/33613820/ but I cannot for the life of me find it again.

nkuehnel commented 5 years ago

Hi Clarissa, you can try using the script that the josm plugin internally uses. I don't know where I originally copied it from though: (find the full file here: https://github.com/matsim-org/josm-matsim-plugin/blob/master/src/main/java/org/matsim/contrib/josm/actions/ShapeExporter.java)

 ```
       Network network = targetScenario.getNetwork();
            CoordinateReferenceSystem crs = MGC.getCRS(ProjectionRegistry.getProjection().toCode());

            Collection<SimpleFeature> features = new ArrayList<>();
            PolylineFeatureFactory linkFactory = new PolylineFeatureFactory.Builder().
                    setCrs(crs).
                    setName("link").
                    addAttribute("ID", String.class).
                    addAttribute("fromID", String.class).
                    addAttribute("toID", String.class).
                    addAttribute("length", Double.class).
                    addAttribute("type", String.class).
                    addAttribute("capacity", Double.class).
                    addAttribute("freespeed", Double.class).
                    create();

            for (Link link : network.getLinks().values()) {
                Coordinate fromNodeCoordinate = new Coordinate(link.getFromNode().getCoord().getX(), link.getFromNode().getCoord().getY());
                Coordinate toNodeCoordinate = new Coordinate(link.getToNode().getCoord().getX(), link.getToNode().getCoord().getY());
                Coordinate linkCoordinate = new Coordinate(link.getCoord().getX(), link.getCoord().getY());
                SimpleFeature ft = linkFactory.createPolyline(new Coordinate [] {fromNodeCoordinate, linkCoordinate, toNodeCoordinate},
                        new Object [] {link.getId().toString(), link.getFromNode().getId().toString(),link.getToNode().getId().toString(), link.getLength(), NetworkUtils.getType(link), link.getCapacity(), link.getFreespeed()}, null);
                features.add(ft);
            }
            ShapeFileWriter.writeGeometries(features, file.getAbsolutePath()+"_links.shp");

            features = new ArrayList<>();
            PointFeatureFactory nodeFactory = new PointFeatureFactory.Builder().
                    setCrs(crs).
                    setName("nodes").
                    addAttribute("ID", String.class).
                    create();

            for (Node node : network.getNodes().values()) {
                SimpleFeature ft = nodeFactory.createPoint(node.getCoord(), new Object[] {node.getId().toString()}, null);
                features.add(ft);
            }
            ShapeFileWriter.writeGeometries(features, file.getAbsolutePath()+"_nodes.shp");
ClarissaLiv commented 5 years ago

PERFECT!

Those were the lines of code I saw earlier today, but paired with the bit in the sourceforge link.

I combined the two and it works beautifully. Here is the completed script:

/**

import java.util.ArrayList; import java.util.Collection;

import org.matsim.api.core.v01.Scenario; import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.network.Network; import org.matsim.api.core.v01.network.Node; 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.core.utils.geometry.geotools.MGC; import org.matsim.core.utils.gis.PointFeatureFactory; import org.matsim.core.utils.gis.PolylineFeatureFactory; import org.matsim.core.utils.gis.ShapeFileWriter; import org.opengis.feature.simple.SimpleFeature; import org.opengis.referencing.crs.CoordinateReferenceSystem;

import com.vividsolutions.jts.geom.Coordinate;

/**

}

kainagel commented 5 years ago

There is Links2ESRIShape in package org.matsim.utils.gis.matsim2esri.network in the matsim matin repo. I just created a reference to this in matsim-code-examples, see here: https://github.com/matsim-org/matsim-code-examples/blob/0.11.x/src/main/java/org/matsim/codeexamples/converter/networkToShapefile/RunConvertNetworkToShapefileExample.java .