6
6
* (C) 2004 Paul Ramsey, [email protected]
7
7
*
8
8
* (C) 2005 Markus Schaber, [email protected]
9
+ *
10
+ * (C) 2015 Phillip Ross, [email protected]
9
11
*
10
12
* This program is free software; you can redistribute it and/or modify it under
11
13
* the terms of the GNU General Public License as published by the Free Software
@@ -43,6 +45,14 @@ public class TestJava2d {
43
45
44
46
public static final Shape [] SHAPEARRAY = new Shape [0 ];
45
47
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
+
46
56
static {
47
57
new Java2DWrapper (); // make shure our driver is initialized
48
58
}
@@ -58,11 +68,14 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
58
68
System .exit (1 );
59
69
}
60
70
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
+ }
64
75
if (geometries .length == 0 ) {
65
- System .err .println ("No geometries were found." );
76
+ if (DEBUG ) {
77
+ System .err .println ("No geometries were read." );
78
+ }
66
79
return ;
67
80
}
68
81
@@ -91,11 +104,15 @@ static Rectangle2D calcbbox(Shape[] geometries) {
91
104
private static Shape [] read (String dburl , String dbuser , String dbpass , String query )
92
105
throws ClassNotFoundException , SQLException {
93
106
ArrayList geometries = new ArrayList ();
94
- System .out .println ("Creating JDBC connection..." );
107
+ if (DEBUG ) {
108
+ System .err .println ("Creating JDBC connection..." );
109
+ }
95
110
Class .forName ("org.postgresql.Driver" );
96
111
Connection conn = DriverManager .getConnection (dburl , dbuser , dbpass );
97
112
98
- System .out .println ("fetching geometries" );
113
+ if (DEBUG ) {
114
+ System .err .println ("fetching geometries: " + query );
115
+ }
99
116
ResultSet r = conn .createStatement ().executeQuery (query );
100
117
101
118
while (r .next ()) {
@@ -123,17 +140,33 @@ public GisCanvas(Shape[] geometries) {
123
140
124
141
public void paint (Graphics og ) {
125
142
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
130
155
AffineTransform at = new AffineTransform ();
131
- at .translate (super . getX () + 5 , super . getY () + 5 );
156
+ at .translate (paddingLeft , paddingTop );
132
157
at .scale (scaleX , scaleY );
133
- at .translate (- bbox . getX (), - bbox . getY () );
158
+ at .translate (offsetX , offsetY );
134
159
135
160
if (DEBUG ) {
136
161
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 );
137
170
System .err .println ("bbox: " + bbox );
138
171
System .err .println ("trans: " + at );
139
172
System .err .println ("new: " + at .createTransformedShape (bbox ).getBounds2D ());
@@ -174,4 +207,4 @@ public void windowIconified(WindowEvent e) {//
174
207
public void windowOpened (WindowEvent e ) {//
175
208
}
176
209
}
177
- }
210
+ }
0 commit comments