Changeset 29299 in osm for applications/editors/josm/plugins/opendata/src
- Timestamp:
- 2013-03-02T00:34:32+01:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
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.