/
SVGWriter.java
711 lines (573 loc) · 23.4 KB
/
SVGWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.wms.svg;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;
import org.geotools.api.data.DataSourceException;
import org.geotools.api.feature.simple.SimpleFeature;
import org.geotools.api.feature.simple.SimpleFeatureType;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPoint;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
/**
* @author Gabriel Roldan
* @version $Id$
*/
class SVGWriter extends OutputStreamWriter {
private static final Logger LOGGER =
org.geotools.util.logging.Logging.getLogger(SVGWriter.class.getPackage().getName());
/**
* a number formatter setted up to write SVG legible numbers ('.' as decimal separator, no group
* separator
*/
private static DecimalFormat formatter;
/** map of geometry class to writer */
private HashMap<Class<? extends Geometry>, SVGFeatureWriter> writers;
static {
Locale locale = new Locale("en", "US");
DecimalFormatSymbols decimalSymbols = new DecimalFormatSymbols(locale);
decimalSymbols.setDecimalSeparator('.');
formatter = new DecimalFormat();
formatter.setDecimalFormatSymbols(decimalSymbols);
// do not group
formatter.setGroupingSize(0);
// do not show decimal separator if it is not needed
formatter.setDecimalSeparatorAlwaysShown(false);
formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
// set default number of fraction digits
formatter.setMaximumFractionDigits(5);
// minimun fraction digits to 0 so they get not rendered if not needed
formatter.setMinimumFractionDigits(0);
}
private double minY;
private double maxY;
private int coordsSkipCount;
private int coordsWriteCount;
private SVGFeatureWriterHandler writerHandler = new SVGFeatureWriterHandler();
private SVGFeatureWriter featureWriter = null;
private double minCoordDistance;
private boolean pointsAsCircles;
/** Creates a new SVGWriter object. */
public SVGWriter(OutputStream out, Envelope mapAreaOfInterest) {
super(out);
this.minY = mapAreaOfInterest.getMinY();
this.maxY = mapAreaOfInterest.getMaxY();
initWriters();
}
private void initWriters() {
writers = new HashMap<>();
writers.put(Point.class, new PointWriter());
writers.put(LineString.class, new LineStringWriter());
writers.put(LinearRing.class, new LineStringWriter());
writers.put(Polygon.class, new PolygonWriter());
writers.put(MultiPoint.class, new MultiPointWriter());
writers.put(MultiLineString.class, new MultiLineStringWriter());
writers.put(MultiPolygon.class, new MultiPolygonWriter());
}
public void setPointsAsCircles(boolean asCircles) {
this.pointsAsCircles = asCircles;
}
public void setGeometryType(Class<?> gtype) {
featureWriter = writers.get(gtype);
if (featureWriter == null) {
// check for abstract Geometry type
if (gtype == Geometry.class) {
featureWriter = new GeometryWriter();
} else {
throw new IllegalArgumentException("No SVG Feature writer defined for " + gtype);
}
}
// if (gtype == Point.class) {
// featureWriter = new PointWriter();
// } else if (gtype == MultiPoint.class) {
// featureWriter = new MultiPointWriter();
// } else if (gtype == LineString.class) {
// featureWriter = new LineStringWriter();
// } else if (gtype == MultiLineString.class) {
// featureWriter = new MultiLineStringWriter();
// } else if (gtype == Polygon.class) {
// featureWriter = new PolygonWriter();
// } else if (gtype == MultiPolygon.class) {
// featureWriter = new MultiPolygonWriter();
// } else {
// throw new IllegalArgumentException(
// "No SVG Feature writer defined for " + gtype);
// }
/*
* if (config.isCollectGeometries()) { this.writerHandler = new
* CollectSVGHandler(featureWriter); } else { this.writerHandler = new
* SVGFeatureWriterHandler(); this.writerHandler = new FIDSVGHandler(this.writerHandler);
* this.writerHandler = new BoundsSVGHandler(this.writerHandler); this.writerHandler = new
* AttributesSVGHandler(this.writerHandler); }
*/
}
public void setWriterHandler(SVGFeatureWriterHandler handler) {
this.writerHandler = handler;
}
public void setMinCoordDistance(double minCoordDistance) {
this.minCoordDistance = minCoordDistance;
}
/**
* if a reference space has been set, returns a translated Y coordinate wich is inverted based
* on the height of such a reference space, otherwise just returns <code>y</code>
*/
public double getY(double y) {
return (maxY - y) + minY;
}
public double getX(double x) {
return x;
}
public void setMaximunFractionDigits(int numDigits) {
formatter.setMaximumFractionDigits(numDigits);
}
public int getMaximunFractionDigits() {
return formatter.getMaximumFractionDigits();
}
public void setMinimunFractionDigits(int numDigits) {
formatter.setMinimumFractionDigits(numDigits);
}
public int getMinimunFractionDigits() {
return formatter.getMinimumFractionDigits();
}
public void write(double d) throws IOException {
write(formatter.format(d));
}
public void write(char c) throws IOException {
super.write(c);
}
public void newline() throws IOException {
super.write('\n');
}
public void writeFeatures(
SimpleFeatureType featureType, SimpleFeatureIterator reader, String style)
throws IOException {
SimpleFeature ft;
try {
Class<?> gtype = featureType.getGeometryDescriptor().getType().getBinding();
boolean doCollect = false;
/*
* boolean doCollect = config.isCollectGeometries() && (gtype != Point.class) && (gtype
* != MultiPoint.class);
*/
setGeometryType(gtype);
setPointsAsCircles("#circle".equals(style));
setUpWriterHandler(featureType, doCollect);
if (doCollect) {
write("<path ");
write("d=\"");
}
while (reader.hasNext()) {
ft = reader.next();
writeFeature(ft);
ft = null;
}
if (doCollect) {
write("\"/>\n");
}
LOGGER.fine("encoded " + featureType.getTypeName());
} catch (Exception ex) {
throw new DataSourceException(ex.getMessage(), ex);
}
}
private void setUpWriterHandler(SimpleFeatureType featureType, boolean doCollect)
throws IOException {
if (doCollect) {
this.writerHandler = new CollectSVGHandler(featureWriter);
LOGGER.finer("Established a collecting features writer handler");
} else {
this.writerHandler = new SVGFeatureWriterHandler();
/*
* REVISIT: get rid of all this attribute stuff, since if attributes are needed it fits
* better to have SVG with gml attributes as another output format for WFS's getFeature.
*/
List atts = new ArrayList(0); // config.getAttributes(typeName);
if (atts.contains("#FID")) {
this.writerHandler = new FIDSVGHandler(this.writerHandler);
atts.remove("#FID");
LOGGER.finer("Added FID handler decorator");
}
if (atts.contains("#BOUNDS")) {
this.writerHandler = new BoundsSVGHandler(this.writerHandler);
atts.remove("#BOUNDS");
LOGGER.finer("Added BOUNDS handler decorator");
}
if (!atts.isEmpty()) {
this.writerHandler = new AttributesSVGHandler(this.writerHandler);
LOGGER.finer("Added ATTRIBUTES handler decorator");
}
}
}
public void writeFeature(SimpleFeature ft) throws IOException {
writerHandler.startFeature(featureWriter, ft);
writerHandler.startGeometry(featureWriter, ft);
writerHandler.writeGeometry(featureWriter, ft);
writerHandler.endGeometry(featureWriter, ft);
writerHandler.endFeature(featureWriter, ft);
}
public class SVGFeatureWriterHandler {
public void startFeature(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
featureWriter.startElement(ft);
}
public void endFeature(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
featureWriter.endElement(ft);
}
public void startGeometry(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
featureWriter.startGeometry((Geometry) ft.getDefaultGeometry());
}
public void writeGeometry(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
featureWriter.writeGeometry((Geometry) ft.getDefaultGeometry());
}
public void endGeometry(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
featureWriter.endGeometry((Geometry) ft.getDefaultGeometry());
}
}
public class CollectSVGHandler extends SVGFeatureWriterHandler {
private SVGFeatureWriter featureWriter;
/** Creates a new CollectSVGHandler object. */
public CollectSVGHandler(SVGFeatureWriter featureWriter) {
this.featureWriter = featureWriter;
}
public void writeFeature(SimpleFeature ft) throws IOException {
featureWriter.writeGeometry((Geometry) ft.getDefaultGeometry());
write('\n');
}
}
/** decorator handler that adds the feature id as the "id" attribute */
public class FIDSVGHandler extends SVGFeatureWriterHandler {
private SVGFeatureWriterHandler handler;
/** Creates a new NormalSVGHandler object. */
public FIDSVGHandler(SVGFeatureWriterHandler handler) {
this.handler = handler;
}
@Override
public void startFeature(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
handler.startFeature(featureWriter, ft);
write(" id=\"");
try {
write(ft.getID());
} catch (IOException ex) {
LOGGER.severe("error getting fid from " + ft);
throw ex;
}
write("\"");
}
}
/** decorator handler that adds the feature id as the "id" attribute */
public class BoundsSVGHandler extends SVGFeatureWriterHandler {
private SVGFeatureWriterHandler handler;
public BoundsSVGHandler(SVGFeatureWriterHandler handler) {
this.handler = handler;
}
@Override
public void startFeature(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
handler.startFeature(featureWriter, ft);
Geometry geom = (Geometry) ft.getDefaultGeometry();
Envelope env = geom.getEnvelopeInternal();
write(" bounds=\"");
write(env.getMinX());
write(' ');
write(env.getMinY());
write(' ');
write(env.getMaxX());
write(' ');
write(env.getMaxY());
write('\"');
}
}
/** decorator handler that adds the feature id as the "id" attribute */
public class AttributesSVGHandler extends SVGFeatureWriterHandler {
private SVGFeatureWriterHandler handler;
public AttributesSVGHandler(SVGFeatureWriterHandler handler) {
this.handler = handler;
}
@Override
public void startFeature(SVGFeatureWriter featureWriter, SimpleFeature ft)
throws IOException {
handler.startFeature(featureWriter, ft);
SimpleFeatureType type = ft.getFeatureType();
int numAtts = type.getAttributeCount();
Object value;
for (int i = 0; i < numAtts; i++) {
value = ft.getAttribute(i);
if ((value != null) && !(value instanceof Geometry)) {
write(' ');
write(type.getDescriptor(i).getName().getLocalPart());
write("=\"");
encodeAttribute(String.valueOf(value));
write('\"');
}
}
}
/**
* Parses the passed string, and encodes the special characters (used in xml for special
* purposes) with the appropriate codes. e.g. '<' is changed to '&lt;'
*
* @param inData The string to encode into xml.
* @task REVISIT: Once we write directly to out, as we should, this method should be
* simpler, as we can just write strings with escapes directly to out, replacing as we
* iterate of chars to write them.
*/
private void encodeAttribute(String inData) throws IOException {
// return null, if null is passed as argument
if (inData == null) {
return;
}
// get the length of input String
int length = inData.length();
char charToCompare;
// iterate over the input String
for (int i = 0; i < length; i++) {
charToCompare = inData.charAt(i);
// if the ith character is special character, replace by code
if (charToCompare == '"') {
write(""");
} else if (charToCompare > 127) {
writeUnicodeEscapeSequence(charToCompare);
} else {
write(charToCompare);
}
}
}
/**
* returns the xml unicode escape sequence for the character <code>c</code>, such as <code>
* "Ñ"</code> for the character <code>'?'</code>
*/
private void writeUnicodeEscapeSequence(char c) throws IOException {
write("&#x");
String hex = Integer.toHexString(c);
int pendingZeros = 4 - hex.length();
for (int i = 0; i < pendingZeros; i++) write('0');
write(hex);
write(';');
}
}
private abstract class SVGFeatureWriter {
protected abstract void startElement(SimpleFeature feature) throws IOException;
protected abstract void startGeometry(Geometry geom) throws IOException;
protected abstract void writeGeometry(Geometry geom) throws IOException;
protected void endGeometry(Geometry geom) throws IOException {
write("\"");
}
protected void endElement(SimpleFeature feature) throws IOException {
write("/>\n");
}
/**
* Writes the content of the <b>d</b> attribute in a <i>path</i> SVG element
*
* <p>While iterating over the coordinate array passed as parameter, this method performs a
* kind of very basic path generalization, verifying that the distance between the current
* coordinate and the last encoded one is greater than the minimun distance expressed by the
* field <code>minCoordDistance</code> and established by the method {@link
* #setReferenceSpace(Envelope, float) setReferenceSpace(Envelope, blurFactor)}
*/
protected void writePathContent(Coordinate[] coords) throws IOException {
write('M');
Coordinate prev = coords[0];
Coordinate curr = null;
write(getX(prev.x));
write(' ');
write(getY(prev.y));
int nCoords = coords.length;
write('l');
for (int i = 1; i < nCoords; i++) {
curr = coords[i];
// let at least 3 points in case it is a polygon
if ((i > 3) && (prev.distance(curr) <= minCoordDistance)) {
++coordsSkipCount;
continue;
}
++coordsWriteCount;
write((getX(curr.x) - getX(prev.x)));
write(' ');
write(getY(curr.y) - getY(prev.y));
write(' ');
prev = curr;
}
}
protected void writeClosedPathContent(Coordinate[] coords) throws IOException {
writePathContent(coords);
write('Z');
}
}
/** */
private class PointWriter extends SVGFeatureWriter {
/** Creates a new PointWriter object. */
public PointWriter() {}
@Override
protected void startElement(SimpleFeature feature) throws IOException {
write(pointsAsCircles ? "<circle r='0.25%' fill='blue'" : "<use");
}
@Override
protected void startGeometry(Geometry geom) throws IOException {}
/**
* overrides writeBounds for points to do nothing. You can get the position of the point
* with the x and y attributes of the "use" SVG element written to represent each point
*/
protected void writeBounds(Envelope env) throws IOException {}
@Override
protected void writeGeometry(Geometry geom) throws IOException {
Point p = (Point) geom;
if (pointsAsCircles) {
write(" cx=\"");
write(getX(p.getX()));
write("\" cy=\"");
write(getY(p.getY()));
} else {
write(" x=\"");
write(getX(p.getX()));
write("\" y=\"");
write(getY(p.getY()));
// Issue GEOS-193, from John Steining.
write("\" xlink:href=\"#point");
// putting this in to fix the issue, but I am not sure about
// the broader implications - I don't think we need it for
// pointsAsCircles. And it looks like the quote gets closed
// somewhere else, but I'm not sure where.
}
}
}
/** */
private class MultiPointWriter extends PointWriter {
public MultiPointWriter() {}
@Override
protected void startElement(SimpleFeature feature) throws IOException {
write("<g ");
}
@Override
protected void startGeometry(Geometry geom) throws IOException {
write("/>\n");
}
@Override
protected void writeGeometry(Geometry geom) throws IOException {
MultiPoint mp = (MultiPoint) geom;
for (int i = 0; i < mp.getNumGeometries(); i++) {
super.startElement(null);
super.writeGeometry(mp.getGeometryN(i));
super.endGeometry(mp.getGeometryN(i));
super.endElement(null);
}
}
@Override
protected void endElement(SimpleFeature feature) throws IOException {
write("</g>\n");
}
}
/**
* Writer to handle feature types which contain a Geometry attribute that is actually of the
* class Geometry. This can occur in heterogeneous data sets.
*
* @author Justin Deoliveira, jdeolive@openplans.org
*/
private class GeometryWriter extends SVGFeatureWriter {
SVGFeatureWriter delegate;
@Override
protected void startElement(SimpleFeature feature) throws IOException {
Geometry g = (Geometry) feature.getDefaultGeometry();
delegate = null;
if (g != null) {
delegate = writers.get(g.getClass());
}
if (delegate == null) {
throw new IllegalArgumentException("No SVG Feature writer defined for " + g);
}
delegate.startElement(feature);
}
@Override
protected void startGeometry(Geometry geom) throws IOException {
delegate.startGeometry(geom);
}
@Override
protected void writeGeometry(Geometry geom) throws IOException {
delegate.writeGeometry(geom);
}
}
/** */
private class LineStringWriter extends SVGFeatureWriter {
/** Creates a new LineStringWriter object. */
public LineStringWriter() {}
@Override
protected void startElement(SimpleFeature feature) throws IOException {
write("<path");
}
@Override
protected void startGeometry(Geometry geom) throws IOException {
write(" d=\"");
}
@Override
protected void writeGeometry(Geometry geom) throws IOException {
writePathContent(geom.getCoordinates());
}
}
/** */
private class MultiLineStringWriter extends LineStringWriter {
/** Creates a new MultiLineStringWriter object. */
public MultiLineStringWriter() {}
@Override
protected void writeGeometry(Geometry geom) throws IOException {
MultiLineString ml = (MultiLineString) geom;
for (int i = 0; i < ml.getNumGeometries(); i++) {
super.writeGeometry(ml.getGeometryN(i));
}
}
}
/** */
private class PolygonWriter extends SVGFeatureWriter {
/** Creates a new PolygonWriter object. */
public PolygonWriter() {}
@Override
protected void startElement(SimpleFeature feature) throws IOException {
write("<path");
}
@Override
protected void startGeometry(Geometry geom) throws IOException {
write(" d=\"");
}
@Override
protected void writeGeometry(Geometry geom) throws IOException {
Polygon poly = (Polygon) geom;
LineString shell = poly.getExteriorRing();
int nHoles = poly.getNumInteriorRing();
writeClosedPathContent(shell.getCoordinates());
for (int i = 0; i < nHoles; i++)
writeClosedPathContent(poly.getInteriorRingN(i).getCoordinates());
}
}
/** */
private class MultiPolygonWriter extends PolygonWriter {
/** Creates a new MultiPolygonWriter object. */
public MultiPolygonWriter() {}
@Override
protected void writeGeometry(Geometry geom) throws IOException {
MultiPolygon mpoly = (MultiPolygon) geom;
for (int i = 0; i < mpoly.getNumGeometries(); i++) {
super.writeGeometry(mpoly.getGeometryN(i));
}
}
}
}