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

Last change on this file since 13727 was 13611, checked in by pieren, 16 years ago

Fix minor issues.

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 ArrayList<Double> fitViewBox = new ArrayList<Double>();
98 ArrayList<ArrayList<EastNorth>> eastNorths = new ArrayList<ArrayList<EastNorth>>();
99 for (int i=0; i< SVGpaths.length; i++) {
100 ArrayList<EastNorth> eastNorth = new ArrayList<EastNorth>();
101 fitViewBox.add( createNodes(SVGpaths[i], eastNorth) );
102 eastNorths.add(eastNorth);
103 }
104 // the smallest fitViewBox indicates the best fitting path in viewBox
105 Double min = Collections.min(fitViewBox);
106 int bestPath = fitViewBox.indexOf(min);
107 List<Node> nodeList = new ArrayList<Node>();
108 for (EastNorth eastNorth : eastNorths.get(bestPath)) {
109 nodeList.add(new Node(Main.proj.eastNorth2latlon(eastNorth)));
110 }
111 Way wayToAdd = new Way();
112 Collection<Command> cmds = new LinkedList<Command>();
113 for (Node node : nodeList) {
114 cmds.add(new AddCommand(node));
115 wayToAdd.nodes.add(node);
116 }
117 wayToAdd.nodes.add(wayToAdd.nodes.get(0)); // close the circle
118 new SimplifyWay().simplifyWay(wayToAdd);
119 cmds.add(new AddCommand(wayToAdd));
120 Main.main.undoRedo.add(new SequenceCommand(tr("Create boundary"), cmds));
121 Main.map.repaint();
122 }
123
124 private double createNodes(String SVGpath, ArrayList<EastNorth> eastNorth) {
125 // looks like "M981283.38 368690.15l143.81 72.46 155.86 ..."
126 String[] coor = SVGpath.split("[MlZ ]"); //coor[1] is x, coor[2] is y
127 double dx = Double.parseDouble(coor[1]);
128 double dy = Double.parseDouble(coor[2]);
129 double minY = Double.MAX_VALUE;
130 double minX = Double.MAX_VALUE;
131 double maxY = Double.MIN_VALUE;
132 double maxX = Double.MIN_VALUE;
133 for (int i=3; i<coor.length; i+=2){
134 double east = dx+=Double.parseDouble(coor[i]);
135 double north = dy+=Double.parseDouble(coor[i+1]);
136 eastNorth.add(new EastNorth(east,north));
137 minX = minX > east ? east : minX;
138 minY = minY > north ? north : minY;
139 maxX = maxX < east ? east : maxX;
140 maxY = maxY < north ? north : maxY;
141 }
142 // flip the image (svg using a reversed Y coordinate system)
143 double pivot = viewBox.min.getY() + (viewBox.max.getY() - viewBox.min.getY()) / 2;
144 for (EastNorth en : eastNorth) {
145 en.setLocation(en.east(), 2 * pivot - en.north());
146 }
147 return Math.abs(minX - viewBox.min.getX())+Math.abs(maxX - viewBox.max.getX())
148 +Math.abs(minY - viewBox.min.getY())+Math.abs(maxY - viewBox.max.getY());
149 }
150
151 private String grabBoundary(EastNorthBound bbox) throws IOException {
152
153 try {
154 URL url = null;
155 url = getURLsvg(bbox);
156 System.out.println("grab:"+url);
157 return grabSVG(url);
158 } catch (MalformedURLException e) {
159 throw (IOException) new IOException(tr("CadastreGrabber: Illegal url.")).initCause(e);
160 }
161 }
162
163 private URL getURLsvg(EastNorthBound bbox) throws MalformedURLException {
164 String str = new String(wmsInterface.baseURL+"/scpc/wms?version=1.1&request=GetMap");
165 str += "&layers=";
166 str += "CDIF:COMMUNE";
167 str += "&format=image/svg";
168 str += "&bbox="+bbox.min.east()+",";
169 str += bbox.min.north() + ",";
170 str += bbox.max.east() + ",";
171 str += bbox.max.north();
172 str += "&width=800&height=600"; // maximum allowed by wms server
173 str += "&styles=";
174 str += "COMMUNE_90";
175 System.out.println("URL="+str);
176 return new URL(str.replace(" ", "%20"));
177 }
178
179 private String grabSVG(URL url) throws IOException {
180 wmsInterface.urlConn = (HttpURLConnection)url.openConnection();
181 wmsInterface.urlConn.setRequestMethod("GET");
182 wmsInterface.setCookie();
183 InputStream is = new ProgressInputStream(wmsInterface.urlConn, Main.pleaseWaitDlg);
184 File file = new File(CadastrePlugin.cacheDir + "boundary.svg");
185 String svg = new String();
186 try {
187 if (file.exists())
188 file.delete();
189 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
190 InputStreamReader isr =new InputStreamReader(is);
191 BufferedReader br = new BufferedReader(isr);
192 String line="";
193 while ( null!=(line=br.readLine())){
194 line += "\n";
195 bos.write(line.getBytes());
196 svg += line;
197 }
198 bos.close();
199 } catch (IOException e) {
200 e.printStackTrace(System.out);
201 }
202 is.close();
203 return svg;
204 }
205
206 public static void download(WMSLayer wmsLayer) {
207 Main.worker.execute(new DownloadSVGTask(wmsLayer));
208 }
209
210}
Note: See TracBrowser for help on using the repository browser.