Skip to content

faq 109319384

Billy Charlton edited this page Sep 5, 2018 · 2 revisions

Creating nodes from shape file

by Sergej Davydov on 2017-05-10 14:20:33


With this script I need to import from lines.shp to network.xml. I generali understand, how I can became line attrubutes. But I dont understand, how take a Nodes (Coordinates and without duplication). All the Nodes I see in "linkAttArray[0][0]" and "linkAttArray[0][0]" (see the code). Its a Multilinestring. 

How I can take Coordinates. Maybe in this way. Maybe another way. 

Thank you.

Here is the code 

package ru.transspot.Tutorial;

 /**
  * Created by sdavydov on 10.05.2017.
  * Create network.xml for MATSim from .shp
  */

 import java.util.Collection;
 import java.util.List;

 import org.matsim.api.core.v01.Scenario;
 import org.matsim.api.core.v01.network.Network;
 import org.matsim.core.config.Config;
 import org.matsim.core.config.ConfigUtils;
 import org.matsim.core.scenario.ScenarioUtils;
 import org.matsim.core.utils.gis.ShapeFileReader;
 import org.opengis.feature.simple.SimpleFeature;

 public class SHP_Import {

     public static void main(String[] args) throws Exception {

         // upload file
         String shpFile = "d:/_Java_Pojects/Matsim-9.0/_Data/forImput/SHP/network.shp";
         // export file
         String xmlFile = "D:/_Java_Pojects/Matsim-9.0/_Data/Export/network.xml";
         //number of links
         int linkNum = 2;
         //number of attributes
         int attNum = 8;
         // array for links and attributes
         String[][] linkAttArray;
         linkAttArray = new String[linkNum][attNum];

         Config config = ConfigUtils.createConfig();
         Scenario scenario = ScenarioUtils.loadScenario(config);
         Network net = scenario.getNetwork();

         ShapeFileReader shapeFileReader = new ShapeFileReader();
         Collection<SimpleFeature> features = shapeFileReader.readFileAndInitialize(shpFile);
         int y = 0;
         for(SimpleFeature feature: features) {
             /**Geometry geometry = (Geometry) feature.getDefaultGeometry();*/
             List<Object> attributes = feature.getAttributes();
             int x = 0;
             for(Object o : attributes) {
                 // put attributes in array
                 linkAttArray[y][x] = o.toString();
                 x++;
             }
             y++;
         }
         //attributes print
         for(int z=0; z<=1; z++){
             System.out.println(linkAttArray[z][0]);
             System.out.println(linkAttArray[z][1] + ", " +
                     linkAttArray[z][2] + ", " + linkAttArray[z][3] + ", " +
                     linkAttArray[z][4] + ", " + linkAttArray[z][5] + ", " +
                     linkAttArray[z][6] + ", " + linkAttArray[z][7]);
         }
     }
 }


 

Multilinrstring is:

MULTILINESTRING ((40086.504182402925 -54517.68242719341, 40086.505322934056 -54517.68242719341, 40086.50609811399 -54517.68287832271, 40086.50668585287 -54517.68242719341, 40086.50796934752 -54517.68243672431))

Line attributes in Screenshot 


Comments: 0

Clone this wiki locally