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