]> git.argeo.org Git - gpl/argeo-suite.git/blob - environment/org.argeo.geo.ui/src/org/argeo/geo/GeoToSvg.java
Improve content layer.
[gpl/argeo-suite.git] / environment / org.argeo.geo.ui / src / org / argeo / geo / GeoToSvg.java
1 package org.argeo.geo;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.Writer;
6 import java.nio.charset.StandardCharsets;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import com.fasterxml.jackson.databind.JsonNode;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14
15 /** Converts a geographical feature to an SVG. */
16 public class GeoToSvg {
17 public void convertGeoJsonToSvg(Path source, Path target) {
18 ObjectMapper objectMapper = new ObjectMapper();
19 try (InputStream in = Files.newInputStream(source);
20 Writer out = Files.newBufferedWriter(target, StandardCharsets.UTF_8)) {
21 JsonNode tree = objectMapper.readTree(in);
22 JsonNode coord = tree.get("features").get(0).get("geometry").get("coordinates");
23 double ratio = 100;
24 double minX = Double.POSITIVE_INFINITY;
25 double maxX = Double.NEGATIVE_INFINITY;
26 double minY = Double.POSITIVE_INFINITY;
27 double maxY = Double.NEGATIVE_INFINITY;
28 List<String> shapes = new ArrayList<>();
29 for (JsonNode shape : coord) {
30 StringBuffer sb = new StringBuffer();
31 sb.append("<polyline style=\"stroke-width:0.00000003;stroke:#000000;\" points=\"");
32 for (JsonNode latlng : shape) {
33 double lat = latlng.get(0).asDouble();
34 double y = lat * ratio;
35 if (y < minY)
36 minY = y;
37 if (y > maxY)
38 maxY = y;
39 double lng = latlng.get(1).asDouble();
40 double x = lng * ratio;
41 if (x < minX)
42 minX = x;
43 if (x > maxX)
44 maxX = x;
45 sb.append(y + "," + x + " ");
46 }
47 sb.append("\">");
48 sb.append("</polyline>\n");
49 shapes.add(sb.toString());
50 }
51
52 double width = maxX - minX;
53 double height = maxY - minY;
54 out.write("<svg xmlns=\"http://www.w3.org/2000/svg\"\n");
55 out.write(" width=\"" + (int) (width * 1000) + "\"\n");
56 out.write(" height=\"" + (int) (height * 1000) + "\"\n");
57 out.write(" viewBox=\"" + minX + "," + minY + "," + width + "," + height + "\"\n");
58 out.write(">\n");
59 for (String shape : shapes) {
60 out.write(shape);
61 out.write("\n");
62 }
63 out.write("</svg>");
64 } catch (IOException e) {
65 throw new RuntimeException("Cannot convert " + source + " to " + target, e);
66 }
67 }
68
69 }