source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/DownloadSVGTask.java@ 13593

Last change on this file since 13593 was 13545, checked in by pieren, 17 years ago

Add municipality boundary import from SVG data delivered by cadastre WMS.

File size: 8.3 KB
Line 
1package cadastre_fr;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.io.BufferedOutputStream;
6import java.io.BufferedReader;
7import java.io.File;
8import java.io.FileOutputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.InputStreamReader;
12import java.net.HttpURLConnection;
13import java.net.MalformedURLException;
14import java.net.URL;
15import java.util.ArrayList;
16import java.util.Collection;
17import java.util.Collections;
18import java.util.LinkedList;
19import java.util.List;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.command.AddCommand;
23import org.openstreetmap.josm.command.Command;
24import org.openstreetmap.josm.command.SequenceCommand;
25import org.openstreetmap.josm.data.coor.EastNorth;
26import org.openstreetmap.josm.data.osm.Node;
27import org.openstreetmap.josm.data.osm.Way;
28import org.openstreetmap.josm.gui.PleaseWaitRunnable;
29import org.openstreetmap.josm.io.ProgressInputStream;
30/**
31 * Grab the SVG administrative boundaries of the active commune layer (cadastre),
32 * isolate the SVG path of the concerned commune (other municipalities are also
33 * downloaded in the SVG data), convert to OSM nodes and way plus simplify.
34 * Thanks to Frederic Rodrigo for his help.
35 */
36public class DownloadSVGTask extends PleaseWaitRunnable {
37
38 private WMSLayer wmsLayer;
39 private CadastreGrabber grabber = CadastrePlugin.cadastreGrabber;
40 private CadastreInterface wmsInterface;
41 private String svg = null;
42 private EastNorthBound viewBox = null;
43
44 public DownloadSVGTask(WMSLayer wmsLayer) {
45 super(tr("Downloading {0}", wmsLayer.name));
46
47 this.wmsLayer = wmsLayer;
48 this.wmsInterface = grabber.getWmsInterface();
49 }
50
51 @Override
52 public void realRun() throws IOException {
53 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting WMS Server..."));
54 try {
55 if (wmsInterface.retrieveInterface(wmsLayer)) {
56 svg = grabBoundary(wmsLayer.getCommuneBBox());
57 if (svg == null)
58 return;
59 Main.pleaseWaitDlg.currentAction.setText(tr("Extract SVG ViewBox..."));
60 getViewBox(svg);
61 if (viewBox == null)
62 return;
63 Main.pleaseWaitDlg.currentAction.setText(tr("Extract best fitting boundary..."));
64 createWay(svg);
65 }
66 } catch (DuplicateLayerException e) {
67 System.err.println("removed a duplicated layer");
68 }
69 }
70
71 @Override
72 protected void cancel() {
73 grabber.getWmsInterface().cancel();
74 }
75
76 @Override
77 protected void finish() {
78 }
79
80 private boolean getViewBox(String svg) {
81 double[] box = new SVGParser().getViewBox(svg);
82 if (box != null) {
83 viewBox = new EastNorthBound(new EastNorth(box[0], box[1]),
84 new EastNorth(box[0]+box[2], box[1]+box[3]));
85 return true;
86 }
87 System.out.println("Unable to parse SVG data (viewBox)");
88 return false;
89 }
90
91 /**
92 * The svg contains more than one commune boundary defined by path elements. So detect
93 * which path element is the best fitting to the viewBox and convert it to OSM objects
94 */
95 private void createWay(String svg) {
96 String[] SVGpaths = new SVGParser().getPaths(svg);
97 //double[] fitViewBox = new double[SVGpaths.length];
98 ArrayList<Double> fitViewBox = new ArrayList<Double>();
99 ArrayList<ArrayList<EastNorth>> eastNorths = new ArrayList<ArrayList<EastNorth>>();
100 for (int i=0; i< SVGpaths.length; i++) {
101 ArrayList<EastNorth> eastNorth = new ArrayList<EastNorth>();
102 fitViewBox.add( createNodes(SVGpaths[i], eastNorth) );;
103 eastNorths.add(eastNorth);
104 }
105 // the smallest fitViewBox indicates the best fitting path in viewBox
106 Double min = Collections.min(fitViewBox);
107 int bestPath = fitViewBox.indexOf(min);
108 List<Node> nodeList = new ArrayList<Node>();
109 for (EastNorth eastNorth : eastNorths.get(bestPath)) {
110 nodeList.add(new Node(Main.proj.eastNorth2latlon(eastNorth)));
111 }
112 Way wayToAdd = new Way();
113 Collection<Command> cmds = new LinkedList<Command>();
114 for (Node node : nodeList) {
115 cmds.add(new AddCommand(node));
116 wayToAdd.nodes.add(node);
117 }
118 wayToAdd.nodes.add(wayToAdd.nodes.get(0)); // close the circle
119 new SimplifyWay().simplifyWay(wayToAdd);
120 cmds.add(new AddCommand(wayToAdd));
121 Main.main.undoRedo.add(new SequenceCommand(tr("Create boundary"), cmds));
122 Main.map.repaint();
123 }
124
125 private double createNodes(String SVGpath, ArrayList<EastNorth> eastNorth) {
126 // looks like "M981283.38 368690.15l143.81 72.46 155.86 ..."
127 String[] coor = SVGpath.split("[MlZ ]"); //coor[1] is x, coor[2] is y
128 double dx = Double.parseDouble(coor[1]);
129 double dy = Double.parseDouble(coor[2]);
130 double minY = Double.MAX_VALUE;
131 double minX = Double.MAX_VALUE;
132 double maxY = Double.MIN_VALUE;
133 double maxX = Double.MIN_VALUE;
134 for (int i=3; i<coor.length; i+=2){
135 double east = dx+=Double.parseDouble(coor[i]);
136 double north = dy+=Double.parseDouble(coor[i+1]);
137 eastNorth.add(new EastNorth(east,north));
138 minX = minX > east ? east : minX;
139 minY = minY > north ? north : minY;
140 maxX = maxX < east ? east : maxX;
141 maxY = maxY < north ? north : maxY;
142 }
143 // flip the image (svg using a reversed Y coordinate system)
144 double pivot = viewBox.min.getY() + (viewBox.max.getY() - viewBox.min.getY()) / 2;
145 for (EastNorth en : eastNorth) {
146 en.setLocation(en.east(), 2 * pivot - en.north());
147 }
148 return Math.abs(minX - viewBox.min.getX())+Math.abs(maxX - viewBox.max.getX())
149 +Math.abs(minY - viewBox.min.getY())+Math.abs(maxY - viewBox.max.getY());
150 }
151
152 private String grabBoundary(EastNorthBound bbox) throws IOException {
153
154 try {
155 URL url = null;
156 url = getURLsvg(bbox);
157 System.out.println("grab:"+url);
158 return grabSVG(url);
159 } catch (MalformedURLException e) {
160 throw (IOException) new IOException(tr("CadastreGrabber: Illegal url.")).initCause(e);
161 }
162 }
163
164 private URL getURLsvg(EastNorthBound bbox) throws MalformedURLException {
165 String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
166 str += "&layers=";
167 str += "CDIF:COMMUNE";
168 str += "&format=image/svg";
169 str += "&bbox="+bbox.min.east()+",";
170 str += bbox.min.north() + ",";
171 str += bbox.max.east() + ",";
172 str += bbox.max.north();
173 str += "&width=800&height=600"; // maximum allowed by wms server
174 str += "&styles=";
175 str += "COMMUNE_90";
176 System.out.println("URL="+str);
177 return new URL(str.replace(" ", "%20"));
178 }
179
180 private String grabSVG(URL url) throws IOException {
181 wmsInterface.urlConn = (HttpURLConnection)url.openConnection();
182 wmsInterface.urlConn.setRequestMethod("GET");
183 wmsInterface.setCookie();
184 InputStream is = new ProgressInputStream(wmsInterface.urlConn, Main.pleaseWaitDlg);
185 File file = new File(CadastrePlugin.cacheDir + "boundary.svg");
186 String svg = new String();
187 try {
188 if (file.exists())
189 file.delete();
190 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
191 InputStreamReader isr =new InputStreamReader(is);
192 BufferedReader br = new BufferedReader(isr);
193 String line="";
194 while ( null!=(line=br.readLine())){
195 line += "\n";
196 bos.write(line.getBytes());
197 svg += line;
198 }
199 bos.close();
200 } catch (IOException e) {
201 e.printStackTrace(System.out);
202 }
203 is.close();
204 return svg;
205 }
206
207 public static void download(WMSLayer wmsLayer) {
208 Main.worker.execute(new DownloadSVGTask(wmsLayer));
209 }
210
211}
Note: See TracBrowser for help on using the repository browser.