1 package nl.tudelft.simulation.dsol.animation.gis.osm;
2
3 import java.awt.Color;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Set;
12
13 import org.djutils.exceptions.Throw;
14
15 import de.siegmar.fastcsv.reader.NamedCsvReader;
16 import de.siegmar.fastcsv.reader.NamedCsvRow;
17 import nl.tudelft.simulation.dsol.animation.gis.FeatureInterface;
18 import nl.tudelft.simulation.dsol.animation.gis.GisMapInterface;
19 import nl.tudelft.simulation.dsol.animation.gis.LayerInterface;
20 import nl.tudelft.simulation.dsol.animation.gis.map.Feature;
21 import nl.tudelft.simulation.dsol.animation.gis.map.GisMap;
22 import nl.tudelft.simulation.dsol.animation.gis.map.Layer;
23 import nl.tudelft.simulation.dsol.animation.gis.parser.ColorParser;
24 import nl.tudelft.simulation.dsol.animation.gis.transform.CoordinateTransform;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public final class OsmFileCsvParser
41 {
42
43 private OsmFileCsvParser()
44 {
45
46 }
47
48
49
50
51
52
53
54
55
56 public static GisMapInterface parseMapFile(final URL csvUrl, final URL osmUrl, final String mapName) throws IOException
57 {
58 return parseMapFile(csvUrl, osmUrl, mapName, new CoordinateTransform.NoTransform());
59 }
60
61
62
63
64
65
66
67
68
69
70 public static GisMapInterface parseMapFile(final URL csvUrl, final URL osmUrl, final String mapName,
71 final CoordinateTransform coordinateTransform) throws IOException
72 {
73 return parseMapFile(csvUrl, osmUrl, mapName, coordinateTransform, ',', '"');
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public static GisMapInterface parseMapFile(final URL csvUrl, final URL osmUrl, final String mapName,
91 final CoordinateTransform coordinateTransform, final char fieldSeparator, final char quoteCharacter)
92 throws IOException
93 {
94 GisMapInterface map = new GisMap();
95 map.setName(mapName);
96 List<String> layerNames = new ArrayList<>();
97 List<LayerInterface> layerList = new ArrayList<>();
98 List<FeatureInterface> featuresToRead = new ArrayList<>();
99
100 Reader reader = new InputStreamReader(csvUrl.openStream());
101 Throw.when(reader == null, IOException.class, "Cannot find CSV file with OSM shape file information at " + csvUrl);
102 NamedCsvReader csvReader =
103 NamedCsvReader.builder().fieldSeparator(fieldSeparator).quoteCharacter(quoteCharacter).build(reader);
104 Set<String> header = csvReader.getHeader();
105 if (!header.contains("layerName") || !header.contains("key") || !header.contains("value")
106 || !header.contains("outlineColor") || !header.contains("fillColor") || !header.contains("display")
107 || !header.contains("transform"))
108 {
109 throw new IOException("OSM GIS map csv-file header row did not contain all column headers\n" + header.toString());
110 }
111
112 Iterator<NamedCsvRow> it = csvReader.iterator();
113 while (it.hasNext())
114 {
115 NamedCsvRow row = it.next();
116
117 String layerName = row.getField("layerName");
118 String key = row.getField("key");
119 String value = row.getField("value");
120 Color outlineColor = ColorParser.parse(row.getField("outlineColor"));
121 Color fillColor = ColorParser.parse(row.getField("fillColor"));
122 boolean display = row.getField("display").toLowerCase().startsWith("t");
123 boolean transform = row.getField("transform").toLowerCase().startsWith("t");
124
125 LayerInterface layer = null;
126 if (layerNames.contains(layerName))
127 {
128 layer = layerList.get(layerNames.indexOf(layerName));
129 }
130 else
131 {
132 layer = new Layer();
133 layerList.add(layer);
134 layer.setName(layerName);
135 layerNames.add(layerName);
136 }
137
138 Feature feature = new Feature();
139 feature.setKey(key);
140 feature.setValue(value);
141 feature.setOutlineColor(outlineColor);
142 feature.setFillColor(fillColor);
143 layer.addFeature(feature);
144 featuresToRead.add(feature);
145 layer.setDisplay(display);
146 layer.setTransform(transform);
147 }
148 reader.close();
149 csvReader.close();
150
151 map.setLayers(layerList);
152 OsmFileReader osmReader = new OsmFileReader(osmUrl, coordinateTransform, featuresToRead);
153 osmReader.populateShapes();
154 return map;
155 }
156
157 }