Changeset 29299 in osm for applications/editors
- Timestamp:
- 2013-03-02T00:34:32+01:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/opendata
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/opendata/modules/fr.datagouvfr/src/org/openstreetmap/josm/plugins/opendata/modules/fr/datagouvfr/datasets/ecologie/AssainissementHandler.java
r28113 r29299 19 19 import java.net.URL; 20 20 import java.util.ArrayList; 21 import java.util.HashSet; 21 22 import java.util.List; 23 import java.util.Map; 24 import java.util.Set; 22 25 23 26 import org.openstreetmap.josm.data.osm.DataSet; 27 import org.openstreetmap.josm.data.osm.Node; 28 import org.openstreetmap.josm.data.osm.Way; 29 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.DefaultSpreadSheetHandler; 30 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.SpreadSheetReader.CoordinateColumns; 24 31 import org.openstreetmap.josm.plugins.opendata.modules.fr.datagouvfr.datasets.DataGouvDataSetHandler; 25 32 import org.openstreetmap.josm.tools.Pair; … … 30 37 super("assainissement-collectif-30381843"); 31 38 setName("Assainissement collectif"); 32 getSpreadSheetHandler().setSheetNumber(1);39 setSpreadSheetHandler(new InternalOdsHandler()); 33 40 } 34 41 … … 40 47 @Override 41 48 public void updateDataSet(DataSet ds) { 42 // TODO Auto-generated method stub49 // Implemented in InternalOdsHandler.nodesAdded() 43 50 } 44 51 … … 51 58 try { 52 59 result.add(new Pair<String, URL>("Données 2009", new URL("http://www.assainissement.developpement-durable.gouv.fr/telecharger2.php"))); 53 // FIXME problem with 2010 file (blank cells ?)54 //result.add(new Pair<String, URL>("Données 2010", new URL("http://www.assainissement.developpement-durable.gouv.fr/telecharger2_2010.php")));60 result.add(new Pair<String, URL>("Données 2010", new URL("http://www.assainissement.developpement-durable.gouv.fr/telecharger2_2010.php"))); 61 result.add(new Pair<String, URL>("Données 2011", new URL("http://www.assainissement.developpement-durable.gouv.fr/telecharger2_2011.php"))); 55 62 } catch (MalformedURLException e) { 56 63 e.printStackTrace(); … … 58 65 return result; 59 66 } 67 68 private final class InternalOdsHandler extends DefaultSpreadSheetHandler { 69 70 private Node nodeWithKeys; 71 72 private final Set<String> interestingKeys = new HashSet<String>(); 73 74 public InternalOdsHandler() { 75 setSheetNumber(1); 76 } 77 78 private void updateTag(Node node, String odsKey, String osmKey) { 79 String value = nodeWithKeys.get(odsKey); 80 removeTag(odsKey); 81 if (value != null && !value.isEmpty()) { 82 addTag(node, osmKey, value); 83 } 84 } 85 86 private void addTag(Node node, String osmKey, String value) { 87 node.put(osmKey, value); 88 interestingKeys.add(osmKey); 89 } 90 91 private void removeTag(String odsKey) { 92 nodeWithKeys.remove(odsKey); 93 } 94 95 private void removeUninterestingTags() { 96 for (String key : nodeWithKeys.getKeys().keySet()) { 97 if (!interestingKeys.contains(key)) { 98 nodeWithKeys.remove(key); 99 } 100 } 101 } 102 103 @Override 104 public void nodesAdded(DataSet ds, Map<CoordinateColumns, Node> nodes, String[] header, int lineNumber) { 105 Node steuNode = null; 106 Node rejetNode = null; 107 for (CoordinateColumns c : nodes.keySet()) { 108 if (header[c.xCol].contains("STEU") && header[c.yCol].contains("STEU")) { 109 steuNode = nodes.get(c); 110 } else if (header[c.xCol].contains("rejet") && header[c.yCol].contains("rejet")) { 111 rejetNode = nodes.get(c); 112 } else { 113 System.err.println("Line "+lineNumber+": Unexpected coordinate columns: "+c); 114 } 115 } 116 if (steuNode == null || rejetNode == null) { 117 System.err.println("Line "+lineNumber+": 'STEU' or 'rejet' information not found"); 118 return; 119 } 120 nodeWithKeys = steuNode.hasKeys() ? steuNode : rejetNode; 121 122 addTag(steuNode, "man_made", "wastewater_plant"); 123 updateTag(steuNode, "Code du STEU", "ref"); 124 updateTag(steuNode, "Nom du STEU", "name"); 125 updateTag(steuNode, "Date de mise en service du STEU", "start_date"); 126 updateTag(steuNode, "Date de mise hors service du STEU", "end_date"); 127 updateTag(steuNode, "Exploitant", "operator"); 128 updateTag(steuNode, "Capacité nominale en EH", "capacity"); 129 updateTag(rejetNode, "Nom du milieu de rejet", "name"); 130 131 Way pipeline = new Way(); 132 pipeline.addNode(steuNode); 133 pipeline.addNode(rejetNode); 134 if (pipeline.getLength() > 1) { 135 //pipeline.put("man_made", "pipeline"); 136 //pipeline.put("type", "water"); 137 ds.addPrimitive(pipeline); 138 } 139 140 removeUninterestingTags(); 141 } 142 } 60 143 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/OdConstants.java
r29014 r29299 132 132 public static final Collection<ProjectionPatterns> PROJECTIONS = new ArrayList<ProjectionPatterns>(); 133 133 134 public static final ProjectionPatterns PRJ_UNKNOWN = new ProjectionPatterns("");135 134 public static final ProjectionPatterns PRJ_WGS84 = new ProjectionPatterns("GPS|WGS84|°décimaux", Projections.getProjectionByCode("EPSG:4326")); 136 135 public static final ProjectionPatterns PRJ_LAMBERT_93 = new ProjectionPatterns("LAMB93|L93", Projections.getProjectionByCode("EPSG:2154")); … … 140 139 public static final ProjectionPatterns PRJ_LAMBERT_2008 = new ProjectionPatterns("LAMB08|LAMB2008", Projections.getProjectionByCode("EPSG:3812")); 141 140 141 // Must always be declared last 142 public static final ProjectionPatterns PRJ_UNKNOWN = new ProjectionPatterns(""); 143 142 144 /** 143 145 * Resources -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/ProjectionPatterns.java
r28050 r29299 69 69 } 70 70 } 71 72 @Override 73 public String toString() { 74 return "[xPattern=" + xPattern + ", yPattern=" + yPattern + ", projection=" + projection + "]"; 75 } 71 76 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/tabular/DefaultSpreadSheetHandler.java
r28364 r29299 16 16 package org.openstreetmap.josm.plugins.opendata.core.io.tabular; 17 17 18 import java.util.Map; 19 18 20 import org.openstreetmap.josm.data.coor.EastNorth; 19 21 import org.openstreetmap.josm.data.coor.LatLon; 22 import org.openstreetmap.josm.data.osm.DataSet; 23 import org.openstreetmap.josm.data.osm.Node; 24 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.SpreadSheetReader.CoordinateColumns; 20 25 21 26 public class DefaultSpreadSheetHandler implements SpreadSheetHandler { … … 82 87 return yCol; 83 88 } 89 90 @Override 91 public void nodesAdded(DataSet ds, Map<CoordinateColumns, Node> nodes, String[] header, int lineNumber) { 92 // To be overriden if needed 93 } 84 94 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/tabular/SpreadSheetHandler.java
r28364 r29299 16 16 package org.openstreetmap.josm.plugins.opendata.core.io.tabular; 17 17 18 import java.util.Map; 19 18 20 import org.openstreetmap.josm.data.coor.EastNorth; 19 21 import org.openstreetmap.josm.data.coor.LatLon; 22 import org.openstreetmap.josm.data.osm.DataSet; 23 import org.openstreetmap.josm.data.osm.Node; 24 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.SpreadSheetReader.CoordinateColumns; 20 25 21 26 public interface SpreadSheetHandler { … … 42 47 43 48 public int getYCol(); 49 50 public void nodesAdded(DataSet ds, Map<CoordinateColumns, Node> nodes, String[] fields, int lineNumber); 44 51 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/tabular/SpreadSheetReader.java
r28891 r29299 24 24 import java.util.ArrayList; 25 25 import java.util.Arrays; 26 import java.util.Collection;27 26 import java.util.HashMap; 27 import java.util.List; 28 28 import java.util.Locale; 29 29 import java.util.Map; 30 30 31 import org.openstreetmap.josm.Main; 31 32 import org.openstreetmap.josm.data.coor.EastNorth; 32 33 import org.openstreetmap.josm.data.osm.DataSet; … … 70 71 } 71 72 72 p rivateclass CoordinateColumns {73 public static class CoordinateColumns { 73 74 public int xCol = -1; 74 75 public int yCol = -1; … … 76 77 return xCol > -1 && yCol > -1; 77 78 } 79 @Override 80 public String toString() { 81 return "[xCol=" + xCol + ", yCol=" + yCol + "]"; 82 } 83 } 84 85 private final CoordinateColumns addCoorColIfNeeded(List<CoordinateColumns> columns, CoordinateColumns col) { 86 if (col == null || col.isOk()) { 87 columns.add(col = new CoordinateColumns()); 88 } 89 return col; 78 90 } 79 91 … … 81 93 System.out.println("Header: "+Arrays.toString(header)); 82 94 83 Map<ProjectionPatterns, CoordinateColumns> projColumns = new HashMap<ProjectionPatterns, CoordinateColumns>(); 84 85 // TODO: faire une liste de coordonnees pour les cas ou plusieurs coordonnées dans la meme projection sont présentes (ex assainissement) 95 Map<ProjectionPatterns, List<CoordinateColumns>> projColumns = new HashMap<ProjectionPatterns, List<CoordinateColumns>>(); 96 86 97 for (int i = 0; i<header.length; i++) { 87 98 for (ProjectionPatterns pp : PROJECTIONS) { 88 CoordinateColumns col = projColumns.get(pp); 99 List<CoordinateColumns> columns = projColumns.get(pp); 100 if (columns == null) { 101 projColumns.put(pp, columns = new ArrayList<CoordinateColumns>()); 102 } 103 CoordinateColumns col = columns.isEmpty() ? null : columns.get(columns.size()-1); 89 104 if (pp.getXPattern().matcher(header[i]).matches()) { 90 if (col == null) { 91 projColumns.put(pp, col = new CoordinateColumns()); 92 } 93 col.xCol = i; 105 addCoorColIfNeeded(columns, col).xCol = i; 106 break; 94 107 } else if (pp.getYPattern().matcher(header[i]).matches()) { 95 if (col == null) { 96 projColumns.put(pp, col = new CoordinateColumns()); 97 } 98 col.yCol = i; 108 addCoorColIfNeeded(columns, col).yCol = i; 109 break; 99 110 } 100 111 } … … 102 113 103 114 Projection proj = null; 104 CoordinateColumns columns = null; 105 Collection<Integer> allProjIndexes = new ArrayList<Integer>(); 115 final List<CoordinateColumns> columns = new ArrayList<CoordinateColumns>(); 106 116 107 117 for (ProjectionPatterns pp : projColumns.keySet()) { 108 CoordinateColumns col = projColumns.get(pp); 109 if (col.isOk()) { 110 if (proj == null) { 111 proj = pp.getProjection(header[col.xCol], header[col.yCol]); 112 columns = col; 113 } 114 allProjIndexes.add(col.xCol); 115 allProjIndexes.add(col.yCol); 116 } 118 for (CoordinateColumns col : projColumns.get(pp)) { 119 if (col.isOk()) { 120 columns.add(col); 121 if (proj == null) { 122 proj = pp.getProjection(header[col.xCol], header[col.yCol]); 123 } 124 } 125 } 117 126 } 118 127 … … 121 130 if (proj != null) { 122 131 // projection identified, do nothing 123 } else if ( columns != null) {132 } else if (!columns.isEmpty()) { 124 133 if (!handlerOK) { 125 134 // TODO: filter proposed projections with min/max values ? … … 135 144 } 136 145 137 System.out.println("Loading data using projection "+proj+" ("+header[columns.xCol]+", "+header[columns.yCol]+")"); 138 146 String message = ""; 147 for (CoordinateColumns c : columns) { 148 if (!message.isEmpty()) { 149 message += "; "; 150 } 151 message += header[c.xCol]+", "+header[c.yCol]; 152 } 153 154 System.out.println("Loading data using projection "+proj+" ("+message+")"); 155 139 156 final DataSet ds = new DataSet(); 140 157 int lineNumber = 1; … … 143 160 while ((fields = readLine(progressMonitor)) != null) { 144 161 lineNumber++; 145 EastNorth en = new EastNorth(Double.NaN, Double.NaN);146 162 if (handler != null) { 147 163 handler.setXCol(-1); 148 164 handler.setYCol(-1); 149 165 } 150 Node n = new Node(); 166 167 final Map<CoordinateColumns, EastNorth> ens = new HashMap<CoordinateColumns, EastNorth>(); 168 final Map<CoordinateColumns, Node> nodes = new HashMap<CoordinateColumns, Node>(); 169 for (CoordinateColumns c : columns) { 170 nodes.put(c, new Node()); 171 ens.put(c, new EastNorth(Double.NaN, Double.NaN)); 172 } 173 151 174 for (int i = 0; i<fields.length; i++) { 152 175 try { 153 176 if (i >= header.length) { 154 177 throw new IllegalArgumentException(tr("Invalid file. Bad length on line {0}. Expected {1} columns, got {2}.", lineNumber, header.length, i+1)); 155 } else if (i == columns.xCol) { 156 en.setLocation(parseDouble(fields[i]), en.north()); 157 if (handler != null) { 158 handler.setXCol(i); 159 } 160 } else if (i == columns.yCol) { 161 en.setLocation(en.east(), parseDouble(fields[i])); 162 if (handler != null) { 163 handler.setYCol(i); 164 } 165 } else if (!allProjIndexes.contains(i)) { 166 if (!fields[i].isEmpty()) { 167 n.put(header[i], fields[i]); 168 } 178 } else { 179 boolean coordinate = false; 180 for (CoordinateColumns c : columns) { 181 EastNorth en = ens.get(c); 182 if (i == c.xCol) { 183 coordinate = true; 184 en.setLocation(parseDouble(fields[i]), en.north()); 185 if (handler != null) { 186 handler.setXCol(i); 187 } 188 } else if (i == c.yCol) { 189 coordinate = true; 190 en.setLocation(en.east(), parseDouble(fields[i])); 191 if (handler != null) { 192 handler.setYCol(i); 193 } 194 } 195 } 196 if (!coordinate) { 197 if (!fields[i].isEmpty()) { 198 nodes.values().iterator().next().put(header[i], fields[i]); 199 } 200 } 169 201 } 170 202 } catch (ParseException e) { … … 172 204 } 173 205 } 174 if (en.isValid()) { 175 n.setCoor(proj != null && !handlerOK ? proj.eastNorth2latlon(en) : handler.getCoor(en, fields)); 176 } else { 177 System.err.println("Warning: Skipping line "+lineNumber+" because no valid coordinates have been found."); 178 } 179 if (n.getCoor() != null) { 180 ds.addPrimitive(n); 206 for (CoordinateColumns c : columns) { 207 Node n = nodes.get(c); 208 EastNorth en = ens.get(c); 209 if (en.isValid()) { 210 n.setCoor(proj != null && !handlerOK ? proj.eastNorth2latlon(en) : handler.getCoor(en, fields)); 211 } else { 212 System.err.println("Warning: Skipping line "+lineNumber+" because no valid coordinates have been found at columns "+c); 213 } 214 if (n.getCoor() != null) { 215 ds.addPrimitive(n); 216 } 217 } 218 if (handler != null && !Main.pref.getBoolean(PREF_RAWDATA)) { 219 handler.nodesAdded(ds, nodes, header, lineNumber); 181 220 } 182 221 }
Note:
See TracChangeset
for help on using the changeset viewer.