Skip to content
Browse files

Add new file an change an existing file to import teleAtlas data set …

…to OSM
  • Loading branch information...
1 parent 6354a7c commit 480c54b52aecbe52f8cc85ea510af2e125ed256b @moiarcsan moiarcsan committed
Showing with 397 additions and 25 deletions.
  1. +346 −0 importer/src/main/java/RestrictionReader.java
  2. +51 −25 importer/src/main/java/ShapeToOSM.java
View
346 importer/src/main/java/RestrictionReader.java
@@ -0,0 +1,346 @@
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.geotools.data.shapefile.ShpFiles;
+import org.geotools.data.shapefile.dbf.DbaseFileReader;
+import org.geotools.data.shapefile.dbf.DbaseFileReader.Row;
+import org.geotools.data.shapefile.shp.ShapefileReader;
+
+import com.vividsolutions.jts.geom.GeometryFactory;
+
+
+public class RestrictionReader {
+
+ public static void processRestrictions(String pathManeuvers, String pathManeuversPath, String pathRestrictions, File file) {
+ ShpFiles shpFile = null;
+ boolean useMemoryMapped = false;
+ GeometryFactory gf = new GeometryFactory();
+ Row rdb = null;
+ Map<String, String> attributes = null;
+ FileWriter fw = null;
+ try {
+ // Open file
+ fw = new FileWriter(file);
+ shpFile = new ShpFiles(pathManeuvers);
+ DbaseFileReader dbfilereader = new DbaseFileReader(shpFile, useMemoryMapped, Charset.defaultCharset());
+ //ShapefileReader shapefilereader = new ShapefileReader(shpFile, true,useMemoryMapped, gf);
+ int num = 0;
+ while (dbfilereader.hasNext()){
+ rdb = dbfilereader.readRow();
+ attributes = getAttributesManeuvers(rdb);
+ // Attributes restrictions
+ if(attributes.get("FEATTYP").equals("2103") && attributes.get("PROMANTYP").equals("0")){
+ long id = Long.valueOf(attributes.get("ID"));
+ long junctID = Long.valueOf(attributes.get("JNCTID"));
+ // Get from way and to way from maneuvers path index table
+ List<String> sequence = getSequence(id, pathManeuversPath);
+ String restrictions = getRestriction(id, pathRestrictions);
+ if(sequence.size() == 2){
+ System.out.println("Restriction nº: " + num);
+ num++;
+ long from = Long.valueOf(sequence.get(0));
+ long to = Long.valueOf(sequence.get(1));
+ String rest = writeRestriction(id, junctID, from, to, restrictions);
+ fw.write(rest);
+ fw.flush();
+ } //TODO length > 2
+ }
+ }
+ // Close files
+ fw.close();
+ dbfilereader.close();
+ //shapefilereader.close();
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ // Method to get Maneuvers attributes from a Maneuvers shape
+ private static Map<String, String> getAttributesManeuvers(Row rdb){
+ Map<String, String> attributes = new HashMap<String, String>();
+ try {
+ attributes.put("ID", rdb.read(0).toString());
+ attributes.put("FEATTYP", rdb.read(1).toString());
+ attributes.put("BIFTYP", rdb.read(2).toString());
+ attributes.put("PROMANTYP", rdb.read(3).toString());
+ attributes.put("JNCTID", rdb.read(4).toString());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return attributes;
+ }
+
+ private static Map<String, String> getAttributesManeuversPath(Row rdb){
+ Map<String, String> attributes = new HashMap<String, String>();
+ try {
+ attributes.put("ID", rdb.read(0).toString());
+ attributes.put("SEQNR", rdb.read(1).toString());
+ attributes.put("TRPELID", rdb.read(2).toString());
+ attributes.put("TRPELTYP", rdb.read(3).toString());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return attributes;
+ }
+
+ private static Map<String, String> getAttributesRestrictions(Row rdb){
+ Map<String, String> attributes = new HashMap<String, String>();
+ try {
+ attributes.put("ID", rdb.read(0).toString());
+ attributes.put("SEQNR", rdb.read(1).toString());
+ attributes.put("FEATTYP", rdb.read(2).toString());
+ attributes.put("RESTRTYP", rdb.read(3).toString());
+ attributes.put("RESTRVAL", rdb.read(4).toString());
+ attributes.put("VT", rdb.read(5).toString());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return attributes;
+ }
+
+ // Method to write a restriction
+ private static String writeRestriction(long idRestriction, long junctID, long from, long to, String tags){
+ String relationHeader = "<relation changeset=\"1\" " +
+ "uid=\"1\" " +
+ "timestamp=\"2012-02-19T19:07:25Z\" " +
+ "version=\"1\" " +
+ "user=\"TeleAtlas2OSM\" " +
+ "id=\"" + idRestriction + "\">\n";
+
+ String memberWayFrom = "<member type=\"way\" ref=\"" + from + "\" role=\"from\"/>\n";
+
+ String memberWayTo = "<member type=\"way\" ref=\"" + to + "\" role=\"to\"/>\n";
+
+ String memberNode = "<member type=\"node\" " +
+ "ref=\"" + junctID + "\" " +
+ "role=\"via\"/>\n";
+
+ String relationTail = "</relation>\n";
+ String tagType = "<tag k=\"type\" v=\"restriction\"/>\n";
+ return relationHeader + memberWayFrom + memberWayTo + memberNode + tagType + tags + relationTail;
+ }
+
+ private static String writeTag(String rt, String rv, String vt){
+ String value[] = new String[2];
+ String tagRestriction = "";
+ String tagExcept = "";
+ if(rt.equals("6Z") || rt.equals("TR") || rt.equals("4B")){
+
+ }else{
+ // Check all posibles cases
+ if(rt.equals("")){
+ value = checkBlank(rv, vt);
+ }else if(rt.equals("BP")){
+ value = checkBP(rv, vt);
+ }else if(rt.equals("RB")){
+ value = checkRB(rv, vt);
+ }else if(rt.equals("SR")){
+ value = checkSR(rv, vt);
+ }else if(rt.equals("8I")){
+ value = check8I(rv, vt);
+ }else if(rt.equals("DF") || rt.equals("6Q")){
+ value = checkDF6Q(rv, vt);
+ }
+ if (value[0] != "") {
+ tagRestriction = "<tag k=\"restriction\" v =\"" + value[0] + "\"/>\n";
+ }
+ if (value[1] != "") {
+ tagExcept = "<tag k=\"except\" v=\"" + value[1] + "\"/>\n";
+ }
+
+ }
+ return tagRestriction + tagExcept;
+ }
+
+ private static String checkVT(String valueIN, String vt){
+ String res = "";
+ // check VT value
+ if (vt.equals("-1")) {
+ res = "";
+ } else if (vt.equals("0")) {
+ // if it's not empty --> ;
+ if (valueIN != "") {
+ res = valueIN + ";bicycle";
+ } else {
+ res = "bicycle";
+ }
+ } else if (vt.equals("11") || vt.equals("12")) {
+ // if it's not psv --> psv;bicycle
+ if (valueIN != "") {
+ if (valueIN != "psv") {
+ res = "psv;bicycle";
+ } else {
+ res = valueIN + ";bicycle";
+ }
+ } else {
+ res = "psv;bicycle";
+ }
+ } else if (vt.equals("16") || vt.equals("17")) {
+ // If it's not empty --> ;
+ if (valueIN != "") {
+ res = valueIN + ";motorcar;bicycle";
+ } else {
+ res = "motorcar;bicycle";
+ }
+ } else if (vt.equals("24")) {
+ if (valueIN != "") {
+ if (valueIN != "psv") {
+ res = "psv;motorcar";
+ } else {
+ res = valueIN + ";motorcar";
+ }
+ } else {
+ res = "psv;motorcar";
+ }
+ }
+ return res;
+ }
+
+ private static String[] checkBlank(String rv, String vt) {
+ String value[] = new String[2];
+ value[0] = "no_straight_on";
+ value[1] = "";
+ // Check VT value
+ value[1] = checkVT(value[1], vt);
+ return value;
+ }
+
+ private static String[] checkDF6Q(String rv, String vt) {
+ String value[] = new String[2];
+ // Check RESTRVAL value
+ if(rv.equals("2")){
+ value[0] = "no_entry";
+ value[1] = "";
+ }else if(rv.equals("3")){
+ value[0] = "no_exit";
+ value[1] = "";
+ }else if(rv.equals("4")){
+ // TODO Two restrictions
+ value[0] = "";
+ value[1] = "";
+ }
+ // Check VT value
+ value[1] = checkVT(value[1], vt);
+ return value;
+ }
+ private static String[] check8I(String rv, String vt) {
+ String value[] = new String[2];
+ // Check RESTRVAL value
+ if(rv.equals("0")){
+ value[0] = "no_straight_on ";
+ value[1] = "";
+ }else if(rv.equals("1")){
+ value[0] = "only_straight_on";
+ value[1] = "";
+ }
+ // Check VT value
+ value[1] = checkVT(value[1], vt);
+ return value;
+ }
+ private static String[] checkSR(String rv, String vt) {
+ String value[] = new String[2];
+ value[0] = "no_entry";
+ value[1] = "psv";
+ // Check VT value
+ value[1] = checkVT(value[1], vt);
+ return value;
+ }
+ private static String[] checkRB(String rv, String vt) {
+ String value[] = new String[2];
+ // Check RESTRVAL value
+ if(rv.equals("1") || rv.equals("3")){
+ value[0] = "no_entry";
+ value[1] = "psv";
+ }else if(rv.equals("2")){
+ value[0] = "no_entry";
+ value[1] = "";
+ }
+ // Check VT value
+ value[1] = checkVT(value[1], vt);
+ return value;
+ }
+ private static String[] checkBP(String rv, String vt) {
+ String value[] = new String[2];
+ // Check RESTRVAL value
+ if(rv.equals("11") || rv.equals("13") || rv.equals("23")){
+ value[0] = "no_entry";
+ value[1] = "psv";
+ }else if(rv.equals("1") || rv.equals("2") || rv.equals("12") || rv.equals("22")){
+ value[0] = "no_entry";
+ value[1] = "";
+ }
+ value[1] = checkVT(value[1], vt);
+
+ return value;
+ }
+ private static List<String> getSequence(long id, String path){
+ List<String> trpelid = new LinkedList<String>();
+ ShpFiles shpFile = null;
+ boolean useMemoryMapped = false;
+ GeometryFactory gf = new GeometryFactory();
+ Row rdb = null;
+ Map<String, String> attributes = null;
+ try {
+ shpFile = new ShpFiles(path);
+ DbaseFileReader dbfilereader = new DbaseFileReader(shpFile, useMemoryMapped, Charset.defaultCharset());
+ //ShapefileReader shapefilereader = new ShapefileReader(shpFile, true,useMemoryMapped, gf);
+ while (dbfilereader.hasNext()){
+ rdb = dbfilereader.readRow();
+ attributes = getAttributesManeuversPath(rdb);
+ long idMP = Long.valueOf(attributes.get("ID"));
+ if(id == idMP){
+ trpelid.add(attributes.get("TRPELID"));
+ }
+ }
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return trpelid;
+ }
+
+ private static String getRestriction(long id, String path){
+ String rest = "";
+ ShpFiles shpFile = null;
+ boolean useMemoryMapped = false;
+ GeometryFactory gf = new GeometryFactory();
+ Row rdb = null;
+ Map<String, String> attributes = null;
+ try {
+ shpFile = new ShpFiles(path);
+ DbaseFileReader dbfilereader = new DbaseFileReader(shpFile, useMemoryMapped, Charset.defaultCharset());
+ //ShapefileReader shapefilereader = new ShapefileReader(shpFile, true,useMemoryMapped, gf);
+ while (dbfilereader.hasNext()){
+ rdb = dbfilereader.readRow();
+ attributes = getAttributesRestrictions(rdb);
+ long idMP = Long.valueOf(attributes.get("ID"));
+ String feattyp = attributes.get("FEATTYP");
+ if(id == idMP){
+ // Restriction to process
+ if(feattyp.equals("2101") || feattyp.equals("2102") || feattyp.equals("2103")){
+ String resttyp = attributes.get("RESTRTYP");
+ String restval = attributes.get("RESTRVAL");
+ String vt = attributes.get("VT");
+ rest = writeTag(resttyp, restval, vt);
+ }
+ }
+ }
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return rest;
+ }
+
+}
View
76 importer/src/main/java/ShapeToOSM.java
@@ -29,7 +29,7 @@
/**
* @param args
*/
- public static void main(String[] args) {
+ public static void main(final String[] args) {
// Shapefile path
String fileName = args[0];
@@ -48,8 +48,20 @@ public static void main(String[] args) {
String way = "";
try {
// Make a temporal file to store the ways
- File tempWays = File.createTempFile("tempWays", null);
- File tempNodes = File.createTempFile("tempNodes", null);
+ final File tempWays = File.createTempFile("tempWays", null);
+ final File tempNodes = File.createTempFile("tempNodes", null);
+ final File tempRestrictions = File.createTempFile(
+ "tempRestrictions", null);
+
+ Thread t = new Thread() {
+ public void run() {
+ RestrictionReader.processRestrictions(args[2], args[3],
+ args[4], tempRestrictions);
+ };
+ };
+
+ t.start();
+
FileWriter frWays = new FileWriter(tempWays);
FileWriter frNodes = new FileWriter(tempNodes);
getNodesFromJunction(args[1], frNodes);
@@ -101,11 +113,15 @@ public static void main(String[] args) {
}
frWays.close();
frNodes.close();
-
+
// Read temporal files to make osm file
- File f = new File(args[2]);
- writeOSMFile(tempWays, tempNodes, f);
-
+ File f = new File(args[5]);
+
+ while(t.isAlive())
+ Thread.sleep(5000);
+
+ writeOSMFile(tempWays, tempNodes, tempRestrictions, f);
+
// Delete temporal file
tempWays.delete();
tempNodes.delete();
@@ -115,33 +131,42 @@ public static void main(String[] args) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
}
-
- public static void writeOSMFile(File ways, File nodes, File fosm){
+
+ public static void writeOSMFile(File ways, File nodes, File restrictions,
+ File fosm) {
FileReader frTempWays;
FileReader frTempNodes;
+ FileReader frTempRestrictions;
FileWriter fwOSM;
String osmHeader = "<osm version='0.6' generator='TeleAtlas2OSM'>\n";
- String osmTile = "</osm>";
+ String osmTail = "</osm>";
try {
// Open files
frTempWays = new FileReader(ways);
frTempNodes = new FileReader(nodes);
+ frTempRestrictions = new FileReader(restrictions);
fwOSM = new FileWriter(fosm);
-
+
// Write osm header
fwOSM.write(osmHeader);
-
+
// Write osm nodes
readToWrite(frTempNodes, fwOSM);
-
+
// Write osm ways
readToWrite(frTempWays, fwOSM);
-
- // Write osm tile
- fwOSM.write(osmTile);
-
+
+ // Write osm restrictions
+ readToWrite(frTempRestrictions, fwOSM);
+
+ // Write osm tile
+ fwOSM.write(osmTail);
+
// Close files
frTempWays.close();
frTempNodes.close();
@@ -150,14 +175,14 @@ public static void writeOSMFile(File ways, File nodes, File fosm){
e.printStackTrace();
}
}
-
- public static void readToWrite(FileReader fr, FileWriter fw){
+
+ public static void readToWrite(FileReader fr, FileWriter fw) {
BufferedReader br = null;
String linea;
- try {
- br = new BufferedReader(fr);
- while((linea=br.readLine())!=null)
- fw.write(linea);
+ try {
+ br = new BufferedReader(fr);
+ while ((linea = br.readLine()) != null)
+ fw.write(linea);
} catch (IOException e) {
e.printStackTrace();
}
@@ -208,7 +233,7 @@ public static void getNodesFromJunction(String path, FileWriter fw) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
- }finally{
+ } finally {
try {
shapefilereader.close();
dbfilereader.close();
@@ -318,7 +343,8 @@ public static String writeWay(Map<String, String> atributos,
return way;
}
- public static void writeNodes(FileWriter fw, long ref, String lat, String lon) {
+ public static void writeNodes(FileWriter fw, long ref, String lat,
+ String lon) {
try {
fw.write("<node id=\""
+ ref

0 comments on commit 480c54b

Please sign in to comment.
Something went wrong with that request. Please try again.