1 package org
.argeo
.app
.geo
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
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
;
12 import com
.fasterxml
.jackson
.databind
.JsonNode
;
13 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
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");
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
;
39 double lng
= latlng
.get(1).asDouble();
40 double x
= lng
* ratio
;
45 sb
.append(y
+ "," + x
+ " ");
48 sb
.append("</polyline>\n");
49 shapes
.add(sb
.toString());
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");
59 for (String shape
: shapes
) {
64 } catch (IOException e
) {
65 throw new RuntimeException("Cannot convert " + source
+ " to " + target
, e
);