Changeset 33003 in osm for applications/editors/josm/plugins/poly
- Timestamp:
- 2016-09-23T21:54:09+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/poly
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/poly/.project
r32286 r33003 16 16 </arguments> 17 17 </buildCommand> 18 <buildCommand> 19 <name>net.sf.eclipsecs.core.CheckstyleBuilder</name> 20 <arguments> 21 </arguments> 22 </buildCommand> 18 23 </buildSpec> 19 24 <natures> 20 25 <nature>org.eclipse.jdt.core.javanature</nature> 26 <nature>net.sf.eclipsecs.core.CheckstyleNature</nature> 21 27 </natures> 22 28 </projectDescription> -
applications/editors/josm/plugins/poly/src/poly/DownloadPolyTask.java
r30357 r33003 1 // License: GPL. For details, see LICENSE file. 1 2 package poly; 2 3 … … 19 20 20 21 @Override 21 public Future<?> download( 22 public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) { 22 23 return null; 23 24 } 24 25 25 26 @Override 26 public Future<?> loadUrl( 27 public Future<?> loadUrl(boolean new_layer, String url, ProgressMonitor progressMonitor) { 27 28 downloadTask = new DownloadTask(new_layer, new ServerPolyReader(url), progressMonitor); 28 29 return Main.worker.submit(downloadTask); … … 42 43 private String url; 43 44 44 public ServerPolyReader( 45 public ServerPolyReader(String url) { 45 46 this.url = url; 46 47 } 47 48 48 49 @Override 49 public DataSet parseOsm( 50 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException { 50 51 try { 51 52 progressMonitor.beginTask(tr("Contacting Server...", 10)); 52 53 return new PolyImporter().parseDataSet(url); 53 } catch (Exception e54 } catch (Exception e) { 54 55 throw new OsmTransferException(e); 55 56 } finally { -
applications/editors/josm/plugins/poly/src/poly/PolyExporter.java
r30738 r33003 1 // License: GPL. For details, see LICENSE file. 1 2 package poly; 2 3 … … 35 36 36 37 @Override 37 public void exportData( 38 if (layer instanceof OsmDataLayer38 public void exportData(File file, Layer layer) throws IOException { 39 if (layer instanceof OsmDataLayer) { 39 40 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"))) { 40 DataSet ds = ((OsmDataLayer)layer).data; 41 DataSet ds = ((OsmDataLayer) layer).data; 41 42 Map<Way, Boolean> ways = new TreeMap<>(); 42 43 String polygonName = file.getName(); 43 if (polygonName.indexOf('.') > 044 if (polygonName.indexOf('.') > 0) 44 45 polygonName = polygonName.substring(0, polygonName.indexOf('.')); 45 for (Way w : ds.getWays()46 if (w.isClosed()46 for (Way w : ds.getWays()) { 47 if (w.isClosed()) { 47 48 boolean outer = isOuter(w); 48 49 ways.put(w, outer); 49 if (w.hasKey("name")50 if (w.hasKey("name")) 50 51 polygonName = w.get("name").replace("\n", " "); 51 52 } … … 56 57 writer.write(polygonName); 57 58 writer.newLine(); 58 for (Way w : ways.keySet()59 if (!ways.get(w)59 for (Way w : ways.keySet()) { 60 if (!ways.get(w)) 60 61 writer.write('!'); 61 62 writer.write(String.valueOf(counter++)); 62 63 writer.newLine(); 63 for (Node n : w.getNodes()64 for (Node n : w.getNodes()) { 64 65 writer.write(String.format(Locale.ENGLISH, " %f %f", n.getCoor().lon(), n.getCoor().lat())); 65 66 writer.newLine(); … … 74 75 } 75 76 76 private boolean isOuter( Way w) {77 for (OsmPrimitive p : w.getReferrers()78 if (p instanceof Relation && ((Relation)p).isMultipolygon()79 for (RelationMember m : ((Relation)p).getMembers()80 if (m.refersTo(w) && "inner".equals(m.getRole())77 private boolean isOuter(Way w) { 78 for (OsmPrimitive p : w.getReferrers()) { 79 if (p instanceof Relation && ((Relation) p).isMultipolygon()) { 80 for (RelationMember m : ((Relation) p).getMembers()) { 81 if (m.refersTo(w) && "inner".equals(m.getRole())) { 81 82 return false; 82 83 } … … 87 88 } 88 89 89 private Map<Way, Boolean> sortOuterInner( 90 private Map<Way, Boolean> sortOuterInner(Map<Way, Boolean> ways) { 90 91 LinkedHashMap<Way, Boolean> result = new LinkedHashMap<>(ways.size()); 91 92 List<Way> inner = new ArrayList<>(); 92 for (Way w : ways.keySet()93 for (Way w : ways.keySet()) { 93 94 Boolean outer = ways.get(w); 94 if ( outer)95 if (outer) 95 96 result.put(w, outer); 96 97 else 97 98 inner.add(w); 98 99 } 99 for (Way w : inner)100 for (Way w : inner) { 100 101 result.put(w, Boolean.FALSE); 102 } 101 103 return result; 102 104 } -
applications/editors/josm/plugins/poly/src/poly/PolyImporter.java
r30738 r33003 1 // License: GPL. For details, see LICENSE file. 1 2 package poly; 2 3 … … 37 38 } 38 39 39 protected DataSet parseDataSet( 40 protected DataSet parseDataSet(final String source) throws IOException, SAXException, IllegalDataException { 40 41 return parseDataSet(new CachedFile(source).getInputStream(), NullProgressMonitor.INSTANCE); 41 42 } 42 43 43 44 @Override 44 protected DataSet parseDataSet( 45 if (progressMonitor == null45 protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException { 46 if (progressMonitor == null) 46 47 progressMonitor = NullProgressMonitor.INSTANCE; 47 48 CheckParameterUtil.ensureParameterNotNull(in, "in"); … … 57 58 progressMonitor.worked(1); 58 59 return ds; 59 } catch (IOException e60 } catch (IOException e) { 60 61 throw new IllegalDataException(tr("Error reading poly file: {0}", e.getMessage()), e); 61 62 } finally { … … 64 65 } 65 66 66 private List<Area> loadPolygon( 67 private List<Area> loadPolygon(BufferedReader reader) throws IllegalDataException, IOException { 67 68 String name = reader.readLine(); 68 if (name == null || name.trim().length() == 069 if (name == null || name.trim().length() == 0) 69 70 throw new IllegalDataException(tr("The file must begin with a polygon name")); 70 71 List<Area> areas = new ArrayList<>(); … … 72 73 boolean parsingSection = false; 73 74 int fixedCoords = 0; 74 while(true) { 75 while (true) { 75 76 String line; 76 77 do { 77 78 line = reader.readLine(); 78 if (line == null79 if (line == null) 79 80 throw new IllegalDataException("File ended prematurely without END record"); 80 81 line = line.trim(); 81 } while (line.length() == 082 83 if (line.equals("END")84 if (!parsingSection82 } while (line.length() == 0); 83 84 if (line.equals("END")) { 85 if (!parsingSection) 85 86 break; 86 87 else { 87 88 // an area has been read 88 if (area.getNodeCount() < 289 if (area.getNodeCount() < 2) 89 90 throw new IllegalDataException(tr("There are less than 2 points in an area")); 90 91 areas.add(area); 91 if (areas.size() == 192 if (areas.size() == 1) 92 93 areas.get(0).setPolygonName(name); 93 94 parsingSection = false; 94 95 } 95 96 } else { 96 if (!parsingSection97 if (!parsingSection) { 97 98 area = new Area(line); 98 99 parsingSection = true; … … 102 103 double[] coords = new double[2]; 103 104 int tokenCount = 0; 104 for (String token : tokens105 if (token.length() > 0106 if (tokenCount > 2105 for (String token : tokens) { 106 if (token.length() > 0) { 107 if (tokenCount > 2) 107 108 throw new IllegalDataException(tr("A polygon coordinate line must contain exactly 2 numbers")); 108 109 try { 109 110 coords[tokenCount++] = Double.parseDouble(token); 110 } catch (NumberFormatException e111 } catch (NumberFormatException e) { 111 112 throw new IllegalDataException(tr("Unable to parse {0} as a number", token)); 112 113 } 113 114 } 114 115 } 115 if (tokenCount < 2116 if (tokenCount < 2) 116 117 throw new IllegalDataException(tr("A polygon coordinate line must contain exactly 2 numbers")); 117 118 LatLon coord = new LatLon(coords[1], coords[0]); 118 if (!coord.isValid()119 if (!coord.isValid()) { 119 120 // fix small deviations 120 121 double lat = coord.lat(); 121 122 double lon = coord.lon(); 122 if (lon < -180.0 && lon> -185.0123 if (lon > 180.0 && lon < 185.0124 if (lat < -90.0 && lat > -95.0125 if (lat > 90.0 && lat < 95.0123 if (lon < -180.0 && lon > -185.0) lon = -180.0; 124 if (lon > 180.0 && lon < 185.0) lon = 180.0; 125 if (lat < -90.0 && lat > -95.0) lat = -90.0; 126 if (lat > 90.0 && lat < 95.0) lat = 90.0; 126 127 fixedCoords++; 127 128 coord = new LatLon(lat, lon); 128 if (!coord.isValid()129 if (!coord.isValid()) 129 130 throw new IllegalDataException(tr("Invalid coordinates were found: {0}, {1}", coord.lat(), coord.lon())); 130 131 } … … 133 134 } 134 135 } 135 if( fixedCoords > 0 ) 136 JOptionPane.showMessageDialog(Main.parent, tr("{0} points were outside world bounds and were moved", fixedCoords), "Import poly", JOptionPane.WARNING_MESSAGE); 136 if (fixedCoords > 0) 137 JOptionPane.showMessageDialog(Main.parent, 138 tr("{0} points were outside world bounds and were moved", fixedCoords), "Import poly", JOptionPane.WARNING_MESSAGE); 137 139 return areas; 138 140 } 139 141 140 private DataSet constructDataSet( 142 private DataSet constructDataSet(List<Area> areas) { 141 143 DataSet ds = new DataSet(); 142 144 ds.setUploadDiscouraged(true); 143 145 144 146 boolean foundInner = false; 145 for (Area area : areas146 if (!area.isOuter()147 for (Area area : areas) { 148 if (!area.isOuter()) 147 149 foundInner = true; 148 150 area.constructWay(ds); 149 151 } 150 152 151 if (foundInner153 if (foundInner) { 152 154 Relation mp = new Relation(); 153 155 mp.put("type", "multipolygon"); 154 for (Area area : areas156 for (Area area : areas) { 155 157 mp.addMember(new RelationMember(area.isOuter() ? "outer" : "inner", area.getWay())); 156 158 } … … 168 170 private Way way; 169 171 170 public Area(String name172 Area(String name) { 171 173 this.name = name; 172 174 outer = name.charAt(0) != '!'; 173 if ( !outer)175 if (!outer) 174 176 this.name = this.name.substring(1); 175 177 nodes = new ArrayList<>(); … … 178 180 } 179 181 180 public void setPolygonName( 182 public void setPolygonName(String polygonName) { 181 183 this.polygonName = polygonName; 182 184 } 183 185 184 public void addNode( 185 if (nodes.isEmpty() || !(nodes.get(nodes.size()-1).equals(node) || nodes.get(0).equals(node))186 public void addNode(LatLon node) { 187 if (nodes.isEmpty() || !(nodes.get(nodes.size()-1).equals(node) || nodes.get(0).equals(node))) 186 188 nodes.add(node); 187 189 } … … 199 201 } 200 202 201 public void constructWay( 203 public void constructWay(DataSet ds) { 202 204 way = new Way(); 203 for (LatLon coord : nodes205 for (LatLon coord : nodes) { 204 206 Node node = new Node(coord); 205 207 ds.addPrimitive(node); … … 207 209 } 208 210 way.addNode(way.getNode(0)); 209 if (polygonName != null211 if (polygonName != null) 210 212 way.put("name", polygonName); 211 213 ds.addPrimitive(way); -
applications/editors/josm/plugins/poly/src/poly/PolyPlugin.java
r32287 r33003 1 // License: GPL. For details, see LICENSE file. 1 2 package poly; 2 3 -
applications/editors/josm/plugins/poly/src/poly/PolyType.java
r28571 r33003 1 // License: GPL. For details, see LICENSE file. 1 2 package poly; 2 3 … … 10 11 */ 11 12 public interface PolyType { 12 public static final String EXTENSION = "poly"; 13 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(EXTENSION, EXTENSION, tr("Osmosis polygon filter files") + " (*." + EXTENSION + ")"); 13 String EXTENSION = "poly"; 14 ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter( 15 EXTENSION, EXTENSION, tr("Osmosis polygon filter files") + " (*." + EXTENSION + ")"); 14 16 }
Note:
See TracChangeset
for help on using the changeset viewer.