Skip to content

Commit 0568b11

Browse files
committed
Merge pull request #28 from phillipross/java2d-point-parsing
Addresses #27 [java2d point parsing]
2 parents 268c3e7 + 834b374 commit 0568b11

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

postgis-jdbc-java2d/src/main/java/examples/TestJava2d.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* (C) 2004 Paul Ramsey, [email protected]
77
*
88
* (C) 2005 Markus Schaber, [email protected]
9+
*
10+
* (C) 2015 Phillip Ross, [email protected]
911
*
1012
* This program is free software; you can redistribute it and/or modify it under
1113
* the terms of the GNU General Public License as published by the Free Software
@@ -43,6 +45,14 @@ public class TestJava2d {
4345

4446
public static final Shape[] SHAPEARRAY = new Shape[0];
4547

48+
public static final String[][] testDataset = new String[][] {
49+
{"point1", "POINT(10 11)"},
50+
{"multipoint1", "MULTIPOINT(10.25 11,10.5 11,10.75 11,11 11,11.25 11,11.5 11,11.75 11,12 11)"},
51+
{"linestring1", "LINESTRING(0 0,100 0,100 100,0 100)"},
52+
{"linestring2", "LINESTRING(-310 110,210 110,210 210,-310 210,-310 110)"},
53+
{"multilinestring", "MULTILINESTRING((0 0,10 10,20 0,30 10),(40 0,40 10,50 10,50 20,60 20))"},
54+
};
55+
4656
static {
4757
new Java2DWrapper(); // make shure our driver is initialized
4858
}
@@ -58,11 +68,14 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
5868
System.exit(1);
5969
}
6070

61-
Shape[] geometries = read(args[0], args[1], args[2], "SELECT " + args[4] + " FROM "
62-
+ args[3]);
63-
71+
Shape[] geometries = read(args[0], args[1], args[2], "SELECT " + args[4] + " FROM " + args[3]);
72+
if (DEBUG) {
73+
System.err.println("read " + geometries.length + " geometries.");
74+
}
6475
if (geometries.length == 0) {
65-
System.err.println("No geometries were found.");
76+
if (DEBUG) {
77+
System.err.println("No geometries were read.");
78+
}
6679
return;
6780
}
6881

@@ -91,11 +104,15 @@ static Rectangle2D calcbbox(Shape[] geometries) {
91104
private static Shape[] read(String dburl, String dbuser, String dbpass, String query)
92105
throws ClassNotFoundException, SQLException {
93106
ArrayList geometries = new ArrayList();
94-
System.out.println("Creating JDBC connection...");
107+
if (DEBUG) {
108+
System.err.println("Creating JDBC connection...");
109+
}
95110
Class.forName("org.postgresql.Driver");
96111
Connection conn = DriverManager.getConnection(dburl, dbuser, dbpass);
97112

98-
System.out.println("fetching geometries");
113+
if (DEBUG) {
114+
System.err.println("fetching geometries: " + query);
115+
}
99116
ResultSet r = conn.createStatement().executeQuery(query);
100117

101118
while (r.next()) {
@@ -123,17 +140,33 @@ public GisCanvas(Shape[] geometries) {
123140

124141
public void paint(Graphics og) {
125142
Graphics2D g = (Graphics2D) og;
126-
127-
final double scaleX = (super.getWidth() - 10) / bbox.getWidth();
128-
final double scaleY = (super.getHeight() - 10) / bbox.getHeight();
129-
143+
// Add 5% padding on all borders
144+
final double paddingTop = bbox.getHeight() * 0.05;
145+
final double paddingBottom = bbox.getHeight() * 0.05;
146+
final double paddingLeft = bbox.getWidth() * 0.05;
147+
final double paddingRight = bbox.getWidth() * 0.05;
148+
// If the bounding box has negative coordinates, we need to offset by the negative coordinate
149+
final double offsetX = (bbox.getX() < 0) ? (0 - bbox.getX()) : 0;
150+
final double offsetY = (bbox.getY() < 0) ? (0 - bbox.getY()) : 0;
151+
// Scale by the bounding box and padding
152+
final double scaleX = (super.getWidth() - (paddingLeft + paddingRight)) / (bbox.getWidth());
153+
final double scaleY = (super.getHeight() - (paddingTop + paddingBottom)) / (bbox.getHeight());
154+
// Apply the transform parameters
130155
AffineTransform at = new AffineTransform();
131-
at.translate(super.getX() + 5, super.getY() + 5);
156+
at.translate(paddingLeft, paddingTop);
132157
at.scale(scaleX, scaleY);
133-
at.translate(-bbox.getX(), -bbox.getY());
158+
at.translate(offsetX, offsetY);
134159

135160
if (DEBUG) {
136161
System.err.println();
162+
System.err.println("paddingTop: " + paddingTop);
163+
System.err.println("paddingBottom: " + paddingBottom);
164+
System.err.println("paddingLeft: " + paddingLeft);
165+
System.err.println("paddingRight: " + paddingRight);
166+
System.err.println("offsetX: " + offsetX);
167+
System.err.println("offsetY: " + offsetY);
168+
System.err.println("scaleX: " + scaleX);
169+
System.err.println("scaleY: " + scaleY);
137170
System.err.println("bbox: " + bbox);
138171
System.err.println("trans: " + at);
139172
System.err.println("new: " + at.createTransformedShape(bbox).getBounds2D());
@@ -174,4 +207,4 @@ public void windowIconified(WindowEvent e) {//
174207
public void windowOpened(WindowEvent e) {//
175208
}
176209
}
177-
}
210+
}

postgis-jdbc-java2d/src/main/java/org/postgis/java2d/PGShapeGeometry.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,11 @@
3939
*
4040
* As the java.awt.Shape methods currently are implemented by using a
4141
* java.awt.geom.GeneralPath object, they have the same semantics.
42-
*
43-
* BUG/TODO: MultiPoints or Points in a Geometry Collection currently don't work
44-
* as expected, as some GeneralPath implementations throw away adjacent MoveTo
45-
* commands as an optimization (e. G. sun 1.5 and ibm 1.5). Points thus are
46-
* translated into MoveTo() followed by a closePath. This may change when we
47-
* implement our own path logics. We have to evaluate whether Graphics2D renders
48-
* a single MoveTo command as a single "brush tip", or we need the closePath()
49-
* command nevertheless to get any drawing. Maybe we need a LineTo() to the same
50-
* coordinages instead.
51-
*
42+
*
43+
* NOTE: (Multi)Points are translated into a sequence of single MoveTo and LineTo
44+
* commands, but WITHOUT a closePath command. When rendering with a stroke that
45+
* is not solid, the points may not be rendered.
46+
*
5247
* (Multi)LineStrings are translated into a sequence of a single MoveTo and
5348
* multiple LineTo vertices, and Polygon rings into a sequence of a single
5449
* MoveTo, multiple LineTo and a closePath command. To allow correct Polygon

postgis-jdbc-java2d/src/main/java/org/postgis/java2d/ShapeBinaryParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ protected int parseGeometry(ValueGetter data, GeneralPath path) {
154154
}
155155

156156
private void parsePoint(ValueGetter data, boolean haveZ, boolean haveM, GeneralPath path) {
157-
path.moveTo((float) data.getDouble(), (float) data.getDouble());
158-
path.closePath();
157+
double x = data.getDouble();
158+
double y = data.getDouble();
159+
path.moveTo(x, y);
160+
path.lineTo(x, y);
159161
skipZM(data, haveZ, haveM);
160162
}
161163

0 commit comments

Comments
 (0)