Revision 2269
trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/WKTParser.java | ||
---|---|---|
1 |
package com.iver.cit.gvsig.fmap.drivers; |
|
2 |
|
|
3 |
import com.iver.cit.gvsig.fmap.core.FGeometry; |
|
4 |
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D; |
|
5 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
|
6 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
|
7 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
|
8 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
|
9 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
10 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
|
11 |
import com.vividsolutions.jts.geom.Coordinate; |
|
12 |
import com.vividsolutions.jts.io.ParseException; |
|
13 |
|
|
14 |
|
|
15 |
import java.awt.geom.Point2D; |
|
16 |
import java.io.IOException; |
|
17 |
import java.io.Reader; |
|
18 |
import java.io.StreamTokenizer; |
|
19 |
import java.io.StringReader; |
|
20 |
import java.util.ArrayList; |
|
21 |
|
|
22 |
/** |
|
23 |
* Converts a Well-Known Text string to a <code>Geometry</code>. |
|
24 |
* <p> |
|
25 |
* The <code>WKTReader</code> allows |
|
26 |
* extracting <code>Geometry</code> objects from either input streams or |
|
27 |
* internal strings. This allows it to function as a parser to read <code>Geometry</code> |
|
28 |
* objects from text blocks embedded in other data formats (e.g. XML). <P> |
|
29 |
* <p> |
|
30 |
* The Well-known |
|
31 |
* Text format is defined in the <A HREF="http://www.opengis.org/techno/specs.htm"> |
|
32 |
* OpenGIS Simple Features Specification for SQL</A> . <P> |
|
33 |
* <p> |
|
34 |
* <B>Note: </B> There is an inconsistency in the SFS. The WKT grammar states |
|
35 |
* that <code>MultiPoints</code> are represented by <code>MULTIPOINT ( ( x y), (x y) )</code> |
|
36 |
* , but the examples show <code>MultiPoint</code>s as <code>MULTIPOINT ( x y, x y )</code> |
|
37 |
* . Other implementations follow the latter syntax, so JTS will adopt it as |
|
38 |
* well. |
|
39 |
* |
|
40 |
* A <code>WKTReader</code> is parameterized by a <code>GeometryFactory</code> |
|
41 |
* , to allow it to create <code>Geometry</code> objects of the appropriate |
|
42 |
* implementation. In particular, the <code>GeometryFactory</code> will |
|
43 |
* determine the <code>PrecisionModel</code> and <code>SRID</code> that is |
|
44 |
* used. <P> |
|
45 |
* |
|
46 |
* The <code>WKTReader</code> will convert the input numbers to the precise |
|
47 |
* internal representation. |
|
48 |
* |
|
49 |
* Reads non-standard "LINEARRING" tags. |
|
50 |
* |
|
51 |
*@version 1.5 |
|
52 |
*/ |
|
53 |
public class WKTParser { |
|
54 |
|
|
55 |
/** |
|
56 |
* Creates a WKTReader that creates objects using a basic GeometryFactory. |
|
57 |
*/ |
|
58 |
public WKTParser() { |
|
59 |
} |
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
/** |
|
64 |
* Converts a Well-known Text representation to a <code>Geometry</code>. |
|
65 |
* |
|
66 |
* @param wellKnownText |
|
67 |
* one or more <Geometry Tagged Text>strings (see the OpenGIS |
|
68 |
* Simple Features Specification) separated by whitespace |
|
69 |
* @return a <code>Geometry</code> specified by <code>wellKnownText</code> |
|
70 |
* @throws ParseException |
|
71 |
* if a parsing problem occurs |
|
72 |
*/ |
|
73 |
public IGeometry read(String wellKnownText) throws ParseException { |
|
74 |
StringReader reader = new StringReader(wellKnownText); |
|
75 |
try { |
|
76 |
return read(reader); |
|
77 |
} |
|
78 |
finally { |
|
79 |
reader.close(); |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
/** |
|
84 |
* Converts a Well-known Text representation to a <code>Geometry</code>. |
|
85 |
* |
|
86 |
*@param reader a Reader which will return a <Geometry Tagged Text> |
|
87 |
* string (see the OpenGIS Simple Features Specification) |
|
88 |
*@return a <code>Geometry</code> read from <code>reader</code> |
|
89 |
*@throws ParseException if a parsing problem occurs |
|
90 |
*/ |
|
91 |
public IGeometry read(Reader reader) throws ParseException { |
|
92 |
StreamTokenizer tokenizer = new StreamTokenizer(reader); |
|
93 |
try { |
|
94 |
return readGeometryTaggedText(tokenizer); |
|
95 |
} |
|
96 |
catch (IOException e) { |
|
97 |
throw new ParseException(e.toString()); |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* Returns the next array of <code>Coordinate</code>s in the stream. |
|
103 |
* |
|
104 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
105 |
* format. The next element returned by the stream should be "(" (the |
|
106 |
* beginning of "(x1 y1, x2 y2, ..., xn yn)") or "EMPTY". |
|
107 |
*@return the next array of <code>Coordinate</code>s in the |
|
108 |
* stream, or an empty array if "EMPTY" is the next element returned by |
|
109 |
* the stream. |
|
110 |
*@throws IOException if an I/O error occurs |
|
111 |
*@throws ParseException if an unexpected token was encountered |
|
112 |
*/ |
|
113 |
private Coordinate[] getCoordinates(StreamTokenizer tokenizer) |
|
114 |
throws IOException, ParseException |
|
115 |
{ |
|
116 |
String nextToken = getNextEmptyOrOpener(tokenizer); |
|
117 |
if (nextToken.equals("EMPTY")) { |
|
118 |
return new Coordinate[]{}; |
|
119 |
} |
|
120 |
ArrayList coordinates = new ArrayList(); |
|
121 |
coordinates.add(getPreciseCoordinate(tokenizer)); |
|
122 |
nextToken = getNextCloserOrComma(tokenizer); |
|
123 |
while (nextToken.equals(",")) { |
|
124 |
coordinates.add(getPreciseCoordinate(tokenizer)); |
|
125 |
nextToken = getNextCloserOrComma(tokenizer); |
|
126 |
} |
|
127 |
Coordinate[] array = new Coordinate[coordinates.size()]; |
|
128 |
return (Coordinate[]) coordinates.toArray(array); |
|
129 |
} |
|
130 |
|
|
131 |
private Coordinate getPreciseCoordinate(StreamTokenizer tokenizer) |
|
132 |
throws IOException, ParseException |
|
133 |
{ |
|
134 |
Coordinate coord = new Coordinate(); |
|
135 |
coord.x = getNextNumber(tokenizer); |
|
136 |
coord.y = getNextNumber(tokenizer); |
|
137 |
if (isNumberNext(tokenizer)) { |
|
138 |
coord.z = getNextNumber(tokenizer); |
|
139 |
} |
|
140 |
return coord; |
|
141 |
} |
|
142 |
private boolean isNumberNext(StreamTokenizer tokenizer) throws IOException { |
|
143 |
try { |
|
144 |
return tokenizer.nextToken() == StreamTokenizer.TT_NUMBER; |
|
145 |
} |
|
146 |
finally { |
|
147 |
tokenizer.pushBack(); |
|
148 |
} |
|
149 |
} |
|
150 |
/** |
|
151 |
* Returns the next number in the stream. |
|
152 |
* |
|
153 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
154 |
* format. The next token must be a number. |
|
155 |
*@return the next number in the stream |
|
156 |
*@throws ParseException if the next token is not a number |
|
157 |
*@throws IOException if an I/O error occurs |
|
158 |
*/ |
|
159 |
private double getNextNumber(StreamTokenizer tokenizer) throws IOException, |
|
160 |
ParseException { |
|
161 |
int type = tokenizer.nextToken(); |
|
162 |
switch (type) { |
|
163 |
case StreamTokenizer.TT_EOF: |
|
164 |
throw new ParseException("Expected number but encountered end of stream"); |
|
165 |
case StreamTokenizer.TT_EOL: |
|
166 |
throw new ParseException("Expected number but encountered end of line"); |
|
167 |
case StreamTokenizer.TT_NUMBER: |
|
168 |
return tokenizer.nval; |
|
169 |
case StreamTokenizer.TT_WORD: |
|
170 |
throw new ParseException("Expected number but encountered word: " + |
|
171 |
tokenizer.sval); |
|
172 |
case '(': |
|
173 |
throw new ParseException("Expected number but encountered '('"); |
|
174 |
case ')': |
|
175 |
throw new ParseException("Expected number but encountered ')'"); |
|
176 |
case ',': |
|
177 |
throw new ParseException("Expected number but encountered ','"); |
|
178 |
} |
|
179 |
return 0; |
|
180 |
} |
|
181 |
|
|
182 |
/** |
|
183 |
* Returns the next "EMPTY" or "(" in the stream as uppercase text. |
|
184 |
* |
|
185 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
186 |
* format. The next token must be "EMPTY" or "(". |
|
187 |
*@return the next "EMPTY" or "(" in the stream as uppercase |
|
188 |
* text. |
|
189 |
*@throws ParseException if the next token is not "EMPTY" or "(" |
|
190 |
*@throws IOException if an I/O error occurs |
|
191 |
*/ |
|
192 |
private String getNextEmptyOrOpener(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
193 |
String nextWord = getNextWord(tokenizer); |
|
194 |
if (nextWord.equals("EMPTY") || nextWord.equals("(")) { |
|
195 |
return nextWord; |
|
196 |
} |
|
197 |
throw new ParseException("Expected 'EMPTY' or '(' but encountered '" + |
|
198 |
nextWord + "'"); |
|
199 |
} |
|
200 |
|
|
201 |
/** |
|
202 |
* Returns the next ")" or "," in the stream. |
|
203 |
* |
|
204 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
205 |
* format. The next token must be ")" or ",". |
|
206 |
*@return the next ")" or "," in the stream |
|
207 |
*@throws ParseException if the next token is not ")" or "," |
|
208 |
*@throws IOException if an I/O error occurs |
|
209 |
*/ |
|
210 |
private String getNextCloserOrComma(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
211 |
String nextWord = getNextWord(tokenizer); |
|
212 |
if (nextWord.equals(",") || nextWord.equals(")")) { |
|
213 |
return nextWord; |
|
214 |
} |
|
215 |
throw new ParseException("Expected ')' or ',' but encountered '" + nextWord |
|
216 |
+ "'"); |
|
217 |
} |
|
218 |
|
|
219 |
/** |
|
220 |
* Returns the next ")" in the stream. |
|
221 |
* |
|
222 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
223 |
* format. The next token must be ")". |
|
224 |
*@return the next ")" in the stream |
|
225 |
*@throws ParseException if the next token is not ")" |
|
226 |
*@throws IOException if an I/O error occurs |
|
227 |
*/ |
|
228 |
private String getNextCloser(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
229 |
String nextWord = getNextWord(tokenizer); |
|
230 |
if (nextWord.equals(")")) { |
|
231 |
return nextWord; |
|
232 |
} |
|
233 |
throw new ParseException("Expected ')' but encountered '" + nextWord + "'"); |
|
234 |
} |
|
235 |
|
|
236 |
/** |
|
237 |
* Returns the next word in the stream as uppercase text. |
|
238 |
* |
|
239 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
240 |
* format. The next token must be a word. |
|
241 |
*@return the next word in the stream as uppercase text |
|
242 |
*@throws ParseException if the next token is not a word |
|
243 |
*@throws IOException if an I/O error occurs |
|
244 |
*/ |
|
245 |
private String getNextWord(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
246 |
int type = tokenizer.nextToken(); |
|
247 |
switch (type) { |
|
248 |
case StreamTokenizer.TT_EOF: |
|
249 |
throw new ParseException("Expected word but encountered end of stream"); |
|
250 |
case StreamTokenizer.TT_EOL: |
|
251 |
throw new ParseException("Expected word but encountered end of line"); |
|
252 |
case StreamTokenizer.TT_NUMBER: |
|
253 |
throw new ParseException("Expected word but encountered number: " + |
|
254 |
tokenizer.nval); |
|
255 |
case StreamTokenizer.TT_WORD: |
|
256 |
return tokenizer.sval.toUpperCase(); |
|
257 |
case '(': |
|
258 |
return "("; |
|
259 |
case ')': |
|
260 |
return ")"; |
|
261 |
case ',': |
|
262 |
return ","; |
|
263 |
} |
|
264 |
// Assert.shouldNeverReachHere("Encountered unexpected StreamTokenizer type: " + type); |
|
265 |
return null; |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Creates a <code>Geometry</code> using the next token in the stream. |
|
270 |
* |
|
271 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
272 |
* format. The next tokens must form a <Geometry Tagged Text>. |
|
273 |
*@return a <code>Geometry</code> specified by the next token |
|
274 |
* in the stream |
|
275 |
*@throws ParseException if the coordinates used to create a <code>Polygon</code> |
|
276 |
* shell and holes do not form closed linestrings, or if an unexpected |
|
277 |
* token was encountered |
|
278 |
*@throws IOException if an I/O error occurs |
|
279 |
*/ |
|
280 |
private IGeometry readGeometryTaggedText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
281 |
String type = getNextWord(tokenizer); |
|
282 |
if (type.equals("POINT")) { |
|
283 |
return readPointText(tokenizer); |
|
284 |
} |
|
285 |
else if (type.equals("LINESTRING")) { |
|
286 |
return readLineStringText(tokenizer); |
|
287 |
} |
|
288 |
else if (type.equals("LINEARRING")) { |
|
289 |
return readLinearRingText(tokenizer); |
|
290 |
} |
|
291 |
else if (type.equals("POLYGON")) { |
|
292 |
return readPolygonText(tokenizer); |
|
293 |
} |
|
294 |
else if (type.equals("MULTIPOINT")) { |
|
295 |
return readMultiPointText(tokenizer); |
|
296 |
} |
|
297 |
else if (type.equals("MULTILINESTRING")) { |
|
298 |
return readMultiLineStringText(tokenizer); |
|
299 |
} |
|
300 |
else if (type.equals("MULTIPOLYGON")) { |
|
301 |
return readMultiPolygonText(tokenizer); |
|
302 |
} |
|
303 |
/* else if (type.equals("GEOMETRYCOLLECTION")) { |
|
304 |
return readGeometryCollectionText(tokenizer); |
|
305 |
} */ |
|
306 |
System.err.println("Unknown type: " + type); |
|
307 |
throw new ParseException("Unknown type: " + type); |
|
308 |
} |
|
309 |
|
|
310 |
/** |
|
311 |
* Creates a <code>Point</code> using the next token in the stream. |
|
312 |
* |
|
313 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
314 |
* format. The next tokens must form a <Point Text>. |
|
315 |
*@return a <code>Point</code> specified by the next token in |
|
316 |
* the stream |
|
317 |
*@throws IOException if an I/O error occurs |
|
318 |
*@throws ParseException if an unexpected token was encountered |
|
319 |
*/ |
|
320 |
private FGeometry readPointText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
321 |
String nextToken = getNextEmptyOrOpener(tokenizer); |
|
322 |
if (nextToken.equals("EMPTY")) { |
|
323 |
return null; |
|
324 |
} |
|
325 |
Coordinate c = getPreciseCoordinate(tokenizer); |
|
326 |
FPoint2D point = new FPoint2D(c.x, c.y ); |
|
327 |
getNextCloser(tokenizer); |
|
328 |
|
|
329 |
return ShapeFactory.createGeometry(point); |
|
330 |
} |
|
331 |
|
|
332 |
/** |
|
333 |
* Creates a <code>LineString</code> using the next token in the stream. |
|
334 |
* |
|
335 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
336 |
* format. The next tokens must form a <LineString Text>. |
|
337 |
*@return a <code>LineString</code> specified by the next |
|
338 |
* token in the stream |
|
339 |
*@throws IOException if an I/O error occurs |
|
340 |
*@throws ParseException if an unexpected token was encountered |
|
341 |
*/ |
|
342 |
private FGeometry readLineStringText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
343 |
Coordinate[] arrayC = getCoordinates(tokenizer); |
|
344 |
GeneralPathX gp = new GeneralPathX(); |
|
345 |
gp.moveTo(arrayC[0].x,arrayC[0].y); |
|
346 |
for (int i=1;i < arrayC.length; i++) |
|
347 |
{ |
|
348 |
gp.lineTo(arrayC[i].x, arrayC[i].y); |
|
349 |
} |
|
350 |
return ShapeFactory.createGeometry(new FPolyline2D(gp)); |
|
351 |
} |
|
352 |
|
|
353 |
/** |
|
354 |
* Creates a <code>LinearRing</code> using the next token in the stream. |
|
355 |
* |
|
356 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
357 |
* format. The next tokens must form a <LineString Text>. |
|
358 |
*@return a <code>LinearRing</code> specified by the next |
|
359 |
* token in the stream |
|
360 |
*@throws IOException if an I/O error occurs |
|
361 |
*@throws ParseException if the coordinates used to create the <code>LinearRing</code> |
|
362 |
* do not form a closed linestring, or if an unexpected token was |
|
363 |
* encountered |
|
364 |
*/ |
|
365 |
private FGeometry readLinearRingText(StreamTokenizer tokenizer) |
|
366 |
throws IOException, ParseException |
|
367 |
{ |
|
368 |
Coordinate[] arrayC = getCoordinates(tokenizer); |
|
369 |
GeneralPathX gp = new GeneralPathX(); |
|
370 |
gp.moveTo(arrayC[0].x, arrayC[0].y); |
|
371 |
for (int i=1;i < arrayC.length; i++) |
|
372 |
{ |
|
373 |
gp.lineTo(arrayC[i].x, arrayC[i].y); |
|
374 |
} |
|
375 |
return ShapeFactory.createGeometry(new FPolygon2D(gp)); |
|
376 |
|
|
377 |
} |
|
378 |
|
|
379 |
/** |
|
380 |
* Creates a <code>MultiPoint</code> using the next token in the stream. |
|
381 |
* |
|
382 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
383 |
* format. The next tokens must form a <MultiPoint Text>. |
|
384 |
*@return a <code>MultiPoint</code> specified by the next |
|
385 |
* token in the stream |
|
386 |
*@throws IOException if an I/O error occurs |
|
387 |
*@throws ParseException if an unexpected token was encountered |
|
388 |
*/ |
|
389 |
private IGeometry readMultiPointText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
390 |
Coordinate[] coords = getCoordinates(tokenizer); |
|
391 |
double[] x = new double[coords.length]; |
|
392 |
double[] y = new double[coords.length]; |
|
393 |
for (int i=0; i < coords.length; i++) |
|
394 |
{ |
|
395 |
x[i] = coords[i].x; |
|
396 |
y[i] = coords[i].y; |
|
397 |
} |
|
398 |
FMultiPoint2D multi = new FMultiPoint2D(x, y); |
|
399 |
return multi; |
|
400 |
} |
|
401 |
|
|
402 |
|
|
403 |
/** |
|
404 |
* Creates a <code>Polygon</code> using the next token in the stream. |
|
405 |
* |
|
406 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
407 |
* format. The next tokens must form a <Polygon Text>. |
|
408 |
*@return a <code>Polygon</code> specified by the next token |
|
409 |
* in the stream |
|
410 |
*@throws ParseException if the coordinates used to create the <code>Polygon</code> |
|
411 |
* shell and holes do not form closed linestrings, or if an unexpected |
|
412 |
* token was encountered. |
|
413 |
*@throws IOException if an I/O error occurs |
|
414 |
*/ |
|
415 |
private FGeometry readPolygonText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
416 |
String nextToken = getNextEmptyOrOpener(tokenizer); |
|
417 |
if (nextToken.equals("EMPTY")) { |
|
418 |
return null; |
|
419 |
} |
|
420 |
ArrayList holes = new ArrayList(); |
|
421 |
FGeometry shell = readLinearRingText(tokenizer); |
|
422 |
nextToken = getNextCloserOrComma(tokenizer); |
|
423 |
while (nextToken.equals(",")) { |
|
424 |
FGeometry hole = readLinearRingText(tokenizer); |
|
425 |
holes.add(hole); |
|
426 |
nextToken = getNextCloserOrComma(tokenizer); |
|
427 |
} |
|
428 |
// LinearRing[] array = new LinearRing[holes.size()]; |
|
429 |
return shell; //geometryFactory.createPolygon(shell, (LinearRing[]) holes.toArray(array)); |
|
430 |
} |
|
431 |
|
|
432 |
/** |
|
433 |
* Creates a <code>MultiLineString</code> using the next token in the stream. |
|
434 |
* |
|
435 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
436 |
* format. The next tokens must form a <MultiLineString Text>. |
|
437 |
*@return a <code>MultiLineString</code> specified by the |
|
438 |
* next token in the stream |
|
439 |
*@throws IOException if an I/O error occurs |
|
440 |
*@throws ParseException if an unexpected token was encountered |
|
441 |
*/ |
|
442 |
private FGeometry readMultiLineStringText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
443 |
// TODO: HACER ESTO BIEN, CON UN GENERAL PATH |
|
444 |
String nextToken = getNextEmptyOrOpener(tokenizer); |
|
445 |
if (nextToken.equals("EMPTY")) { |
|
446 |
return null; |
|
447 |
} |
|
448 |
ArrayList lineStrings = new ArrayList(); |
|
449 |
FGeometry lineString = readLineStringText(tokenizer); |
|
450 |
lineStrings.add(lineString); |
|
451 |
nextToken = getNextCloserOrComma(tokenizer); |
|
452 |
while (nextToken.equals(",")) { |
|
453 |
lineString = readLineStringText(tokenizer); |
|
454 |
lineStrings.add(lineString); |
|
455 |
nextToken = getNextCloserOrComma(tokenizer); |
|
456 |
} |
|
457 |
// LineString[] array = new LineString[lineStrings.size()]; |
|
458 |
return lineString; // geometryFactory.createMultiLineString((LineString[]) lineStrings.toArray(array)); |
|
459 |
} |
|
460 |
|
|
461 |
/** |
|
462 |
* Creates a <code>MultiPolygon</code> using the next token in the stream. |
|
463 |
* |
|
464 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
465 |
* format. The next tokens must form a <MultiPolygon Text>. |
|
466 |
*@return a <code>MultiPolygon</code> specified by the next |
|
467 |
* token in the stream, or if if the coordinates used to create the |
|
468 |
* <code>Polygon</code> shells and holes do not form closed linestrings. |
|
469 |
*@throws IOException if an I/O error occurs |
|
470 |
*@throws ParseException if an unexpected token was encountered |
|
471 |
*/ |
|
472 |
// TODO: |
|
473 |
private IGeometry readMultiPolygonText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
474 |
String nextToken = getNextEmptyOrOpener(tokenizer); |
|
475 |
if (nextToken.equals("EMPTY")) { |
|
476 |
return null; |
|
477 |
} |
|
478 |
ArrayList polygons = new ArrayList(); |
|
479 |
FGeometry polygon = readPolygonText(tokenizer); |
|
480 |
/* polygons.add(polygon); |
|
481 |
nextToken = getNextCloserOrComma(tokenizer); |
|
482 |
while (nextToken.equals(",")) { |
|
483 |
polygon = readPolygonText(tokenizer); |
|
484 |
polygons.add(polygon); |
|
485 |
nextToken = getNextCloserOrComma(tokenizer); |
|
486 |
} */ |
|
487 |
// Polygon[] array = new Polygon[polygons.size()]; |
|
488 |
return polygon; //geometryFactory.createMultiPolygon((Polygon[]) polygons.toArray(array)); |
|
489 |
} |
|
490 |
|
|
491 |
/** |
|
492 |
* Creates a <code>GeometryCollection</code> using the next token in the |
|
493 |
* stream. |
|
494 |
* |
|
495 |
*@param tokenizer tokenizer over a stream of text in Well-known Text |
|
496 |
* format. The next tokens must form a <GeometryCollection Text>. |
|
497 |
*@return a <code>GeometryCollection</code> specified by the |
|
498 |
* next token in the stream |
|
499 |
*@throws ParseException if the coordinates used to create a <code>Polygon</code> |
|
500 |
* shell and holes do not form closed linestrings, or if an unexpected |
|
501 |
* token was encountered |
|
502 |
*@throws IOException if an I/O error occurs |
|
503 |
*/ |
|
504 |
// TODO: |
|
505 |
/* private GeometryCollection readGeometryCollectionText(StreamTokenizer tokenizer) throws IOException, ParseException { |
|
506 |
String nextToken = getNextEmptyOrOpener(tokenizer); |
|
507 |
if (nextToken.equals("EMPTY")) { |
|
508 |
return geometryFactory.createGeometryCollection(new Geometry[]{}); |
|
509 |
} |
|
510 |
ArrayList geometries = new ArrayList(); |
|
511 |
Geometry geometry = readGeometryTaggedText(tokenizer); |
|
512 |
geometries.add(geometry); |
|
513 |
nextToken = getNextCloserOrComma(tokenizer); |
|
514 |
while (nextToken.equals(",")) { |
|
515 |
geometry = readGeometryTaggedText(tokenizer); |
|
516 |
geometries.add(geometry); |
|
517 |
nextToken = getNextCloserOrComma(tokenizer); |
|
518 |
} |
|
519 |
Geometry[] array = new Geometry[geometries.size()]; |
|
520 |
return geometryFactory.createGeometryCollection((Geometry[]) geometries.toArray(array)); |
|
521 |
} */ |
|
522 |
} |
|
523 |
|
|
0 | 524 |
trunk/libraries/libFMap/.classpath | ||
---|---|---|
8 | 8 |
<attributes> |
9 | 9 |
</attributes> |
10 | 10 |
</classpathentry> |
11 |
<classpathentry kind="lib" path="lib/log4j-1.2.8.jar"> |
|
12 |
<attributes> |
|
13 |
</attributes> |
|
14 |
</classpathentry> |
|
15 | 11 |
<classpathentry kind="lib" path="lib/geoapi-SNAPSHOT.jar"> |
16 | 12 |
<attributes> |
17 | 13 |
</attributes> |
... | ... | |
80 | 76 |
<attributes> |
81 | 77 |
</attributes> |
82 | 78 |
</classpathentry> |
83 |
<classpathentry kind="lib" path="lib/gdbms-0.7.jar"> |
|
84 |
<attributes> |
|
85 |
</attributes> |
|
86 |
</classpathentry> |
|
87 | 79 |
<classpathentry kind="lib" path="lib/jts-1.6.jar"> |
88 | 80 |
<attributes> |
89 | 81 |
</attributes> |
... | ... | |
96 | 88 |
<attributes> |
97 | 89 |
</attributes> |
98 | 90 |
</classpathentry> |
91 |
<classpathentry kind="lib" path="lib/gdbms-0.8-SNAPSHOT.jar"> |
|
92 |
<attributes> |
|
93 |
</attributes> |
|
94 |
</classpathentry> |
|
95 |
<classpathentry kind="lib" path="/Andami/lib/log4j-1.2.8.jar"> |
|
96 |
<attributes> |
|
97 |
</attributes> |
|
98 |
</classpathentry> |
|
99 | 99 |
<classpathentry kind="output" path="bin"/> |
100 | 100 |
</classpath> |
trunk/extensions/extJDBC/.cvsignore | ||
---|---|---|
1 |
bin |
|
0 | 2 |
trunk/extensions/extJDBC/build.xml | ||
---|---|---|
1 |
<project name="Generar extension en Andami" default="generate-without-source" basedir="."> |
|
2 |
<description> |
|
3 |
Instala el plugin en Andami |
|
4 |
</description> |
|
5 |
<!-- set global properties for this build --> |
|
6 |
<property name="src" location="src"/> |
|
7 |
<property name="build" location="bin"/> |
|
8 |
<property name="dist" location="dist"/> |
|
9 |
<property name="mainplugin" value="com.iver.cit.gvsig"/> |
|
10 |
<property name="plugin" value="com.iver.cit.gvsig.jdbc_spatial"/> |
|
11 |
<property name="gvsiglibjar" value="gvsig-jdbcspatial"/> |
|
12 |
<property name="fmapjar" value="fmap-jdbcspatial"/> |
|
13 |
<property name="driverjar" value="jdbc"/> |
|
14 |
<property name="extensionDir" location="../Andami/gvSIG/extensiones"/> |
|
15 |
<property name="drivers-dir" location="${extensionDir}/${mainplugin}/drivers" /> |
|
16 |
|
|
17 |
<target name="init"> |
|
18 |
<!-- Create the time stamp --> |
|
19 |
<tstamp/> |
|
20 |
<!-- Create the build directory structure used by compile --> |
|
21 |
<mkdir dir="${build}"/> |
|
22 |
<mkdir dir="${dist}"/> |
|
23 |
|
|
24 |
</target> |
|
25 |
|
|
26 |
<target name="generate-without-source" |
|
27 |
description="generate the distribution without the source file"> |
|
28 |
<!-- Create the distribution directory --> |
|
29 |
<mkdir dir="${dist}"/> |
|
30 |
<mkdir dir="${drivers-dir}/jdbc_spatial" /> |
|
31 |
|
|
32 |
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> |
|
33 |
<jar jarfile="${dist}/${plugin}.jar" basedir="${build}" includes="com/iver/cit/gvsig/**"/> |
|
34 |
<copy file="config/config.xml" todir="${dist}"/> |
|
35 |
<copy todir="${dist}"> |
|
36 |
<fileset dir="." includes="text*.properties"/> |
|
37 |
</copy> |
|
38 |
<!-- <copy todir="${dist}"> |
|
39 |
<fileset dir="./lib" includes="*.jar,*.zip"/> |
|
40 |
<fileset dir="." includes=".keystore"/> |
|
41 |
</copy> --> |
|
42 |
<move todir="${extensionDir}/${mainplugin}/lib"> |
|
43 |
<fileset dir="${dist}" includes="${gvsiglibjar}.jar"/> |
|
44 |
<fileset dir="${dist}" includes="${fmapjar}.jar"/> |
|
45 |
</move> |
|
46 |
<move todir="${extensionDir}/${plugin}/"> |
|
47 |
<fileset dir="${dist}" includes="**/**"/> |
|
48 |
</move> |
|
49 |
</target> |
|
50 |
|
|
51 |
|
|
52 |
<target name="generate-with-source" description="generate the distribution with the source file" > |
|
53 |
<!-- Create the distribution directory --> |
|
54 |
<mkdir dir="${dist}"/> |
|
55 |
|
|
56 |
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> |
|
57 |
<jar jarfile="${dist}/${plugin}.jar" basedir="${build}"/> |
|
58 |
<copy file="config/config.xml" todir="${dist}"/> |
|
59 |
<copy todir="${dist}"> |
|
60 |
<fileset dir="." includes="text*.properties"/> |
|
61 |
</copy> |
|
62 |
<copy todir="${dist}"> |
|
63 |
<fileset dir="." includes="${src}"/> |
|
64 |
</copy> |
|
65 |
<copy todir="${dist}/images"> |
|
66 |
<fileset dir="images/" includes="*"/> |
|
67 |
</copy> |
|
68 |
<move todir="${extension-dir}/${plugin}/"> |
|
69 |
<fileset dir="${dist}" includes="**/**"/> |
|
70 |
</move> |
|
71 |
</target> |
|
72 |
|
|
73 |
<target name="clean" |
|
74 |
description="clean up" > |
|
75 |
<!-- Delete the ${build} and ${dist} directory trees --> |
|
76 |
<delete dir="${dist}"/> |
|
77 |
</target> |
|
78 |
</project> |
|
79 |
|
|
0 | 80 |
trunk/extensions/extJDBC/.classpath | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<classpath> |
|
3 |
<classpathentry kind="src" path="src"> |
|
4 |
<attributes> |
|
5 |
</attributes> |
|
6 |
</classpathentry> |
|
7 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> |
|
8 |
<attributes> |
|
9 |
</attributes> |
|
10 |
</classpathentry> |
|
11 |
<classpathentry combineaccessrules="false" kind="src" path="/Andami"> |
|
12 |
<attributes> |
|
13 |
</attributes> |
|
14 |
</classpathentry> |
|
15 |
<classpathentry combineaccessrules="false" kind="src" path="/FMap 03"> |
|
16 |
<attributes> |
|
17 |
</attributes> |
|
18 |
</classpathentry> |
|
19 |
<classpathentry sourcepath="/Utiles/src" kind="lib" path="/Andami/lib/iver-utiles.jar"> |
|
20 |
<attributes> |
|
21 |
</attributes> |
|
22 |
</classpathentry> |
|
23 |
<classpathentry sourcepath="/DriverManager/src" kind="lib" path="/FMap 03/lib/driver-manager-1.0.jar"> |
|
24 |
<attributes> |
|
25 |
</attributes> |
|
26 |
</classpathentry> |
|
27 |
<classpathentry kind="lib" path="/FMap 03/lib/gdbms-0.8-SNAPSHOT.jar"> |
|
28 |
<attributes> |
|
29 |
</attributes> |
|
30 |
</classpathentry> |
|
31 |
<classpathentry combineaccessrules="false" kind="src" path="/gvSIG 03"> |
|
32 |
<attributes> |
|
33 |
</attributes> |
|
34 |
</classpathentry> |
|
35 |
<classpathentry sourcepath="/jCMS" kind="lib" path="/FMap 03/lib/cms.jar"> |
|
36 |
<attributes> |
|
37 |
</attributes> |
|
38 |
</classpathentry> |
|
39 |
<classpathentry kind="lib" path="/Andami/lib/log4j-1.2.8.jar"> |
|
40 |
<attributes> |
|
41 |
</attributes> |
|
42 |
</classpathentry> |
|
43 |
<classpathentry kind="lib" path="/FMap 03/lib/postgis-jdbc-driver.jar"> |
|
44 |
<attributes> |
|
45 |
</attributes> |
|
46 |
</classpathentry> |
|
47 |
<classpathentry kind="output" path="bin"/> |
|
48 |
</classpath> |
|
0 | 49 |
trunk/extensions/extJDBC/.project | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<projectDescription> |
|
3 |
<name>gvSIG_JDBC</name> |
|
4 |
<comment></comment> |
|
5 |
<projects> |
|
6 |
</projects> |
|
7 |
<buildSpec> |
|
8 |
<buildCommand> |
|
9 |
<name>org.eclipse.jdt.core.javabuilder</name> |
|
10 |
<arguments> |
|
11 |
</arguments> |
|
12 |
</buildCommand> |
|
13 |
</buildSpec> |
|
14 |
<natures> |
|
15 |
<nature>org.eclipse.jdt.core.javanature</nature> |
|
16 |
</natures> |
|
17 |
</projectDescription> |
|
0 | 18 |
trunk/extensions/extJDBC/config/config.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="ISO-8859-1"?> |
|
2 |
<plugin-config> |
|
3 |
<depends plugin-name="com.iver.cit.gvsig" /> |
|
4 |
<libraries library-dir="."/> |
|
5 |
<extensions> |
|
6 |
<extension class-name="com.iver.cit.gvsig.jdbc_spatial.ExtJDBC_Spatial" |
|
7 |
description="Support to access JDBC spatial databases PostgreSQL and mySQL" |
|
8 |
active="true" |
|
9 |
priority="1"> |
|
10 |
</extension> |
|
11 |
</extensions> |
|
12 |
</plugin-config> |
|
0 | 13 |
trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/mysql/MySQLDriver.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 04-mar-2005 |
|
3 |
* |
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
5 |
* |
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
7 |
* |
|
8 |
* This program is free software; you can redistribute it and/or |
|
9 |
* modify it under the terms of the GNU General Public License |
|
10 |
* as published by the Free Software Foundation; either version 2 |
|
11 |
* of the License, or (at your option) any later version. |
|
12 |
* |
|
13 |
* This program is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with this program; if not, write to the Free Software |
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
* |
|
22 |
* For more information, contact: |
|
23 |
* |
|
24 |
* Generalitat Valenciana |
|
25 |
* Conselleria d'Infraestructures i Transport |
|
26 |
* Av. Blasco Ib??ez, 50 |
|
27 |
* 46010 VALENCIA |
|
28 |
* SPAIN |
|
29 |
* |
|
30 |
* +34 963862235 |
|
31 |
* gvsig@gva.es |
|
32 |
* www.gvsig.gva.es |
|
33 |
* |
|
34 |
* or |
|
35 |
* |
|
36 |
* IVER T.I. S.A |
|
37 |
* Salamanca 50 |
|
38 |
* 46005 Valencia |
|
39 |
* Spain |
|
40 |
* |
|
41 |
* +34 963163400 |
|
42 |
* dac@iver.es |
|
43 |
*/ |
|
44 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql; |
|
45 |
|
|
46 |
import java.awt.geom.Rectangle2D; |
|
47 |
import java.sql.Connection; |
|
48 |
import java.sql.ResultSet; |
|
49 |
import java.sql.SQLException; |
|
50 |
import java.sql.Statement; |
|
51 |
|
|
52 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
|
53 |
import com.iver.cit.gvsig.fmap.DriverException; |
|
54 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
55 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver; |
|
56 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
|
57 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
|
58 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser; |
|
59 |
|
|
60 |
/** |
|
61 |
* @author FJP |
|
62 |
* |
|
63 |
* TODO To change the template for this generated type comment go to |
|
64 |
* Window - Preferences - Java - Code Generation - Code and Comments |
|
65 |
*/ |
|
66 |
public class MySQLDriver extends DefaultDBDriver { |
|
67 |
private WKBParser parser = new WKBParser(); |
|
68 |
/* private int fetch_min=-1; |
|
69 |
private int fetch_max=-1; */ |
|
70 |
private Statement st; |
|
71 |
private Rectangle2D fullExtent = null; |
|
72 |
private String strAux; |
|
73 |
private Rectangle2D workingArea; |
|
74 |
|
|
75 |
|
|
76 |
/** |
|
77 |
* |
|
78 |
*/ |
|
79 |
public MySQLDriver() { |
|
80 |
} |
|
81 |
/* (non-Javadoc) |
|
82 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes() |
|
83 |
*/ |
|
84 |
public DriverAttributes getDriverAttributes() { |
|
85 |
return null; |
|
86 |
} |
|
87 |
|
|
88 |
/* (non-Javadoc) |
|
89 |
* @see com.hardcode.driverManager.Driver#getName() |
|
90 |
*/ |
|
91 |
public String getName() { |
|
92 |
return "mySQL JDBC Driver"; |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int) |
|
97 |
*/ |
|
98 |
public IGeometry getShape(int index) { |
|
99 |
IGeometry geom = null; |
|
100 |
boolean resul; |
|
101 |
try { |
|
102 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS |
|
103 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE |
|
104 |
// LO HAGA |
|
105 |
// System.out.println("getShape " + index); |
|
106 |
/* if (index < fetch_min) |
|
107 |
{ |
|
108 |
rs.close(); |
|
109 |
|
|
110 |
rs = st.executeQuery(sqlOrig); |
|
111 |
fetch_min = 0; |
|
112 |
fetch_max = rs.getFetchSize(); |
|
113 |
} |
|
114 |
while (index >= fetch_max) |
|
115 |
{ |
|
116 |
rs.last(); |
|
117 |
// forzamos una carga |
|
118 |
rs.next(); |
|
119 |
fetch_min = fetch_max; |
|
120 |
fetch_max = fetch_max + rs.getFetchSize(); |
|
121 |
// System.out.println("fetchSize = " + rs.getFetchSize() + " " + fetch_min + "-" + fetch_max); |
|
122 |
} |
|
123 |
rs.absolute(index+1 - fetch_min); */ |
|
124 |
rs.absolute(index+1); |
|
125 |
// strAux = rs.getString(1); |
|
126 |
// geom = parser.read(strAux); |
|
127 |
byte[] data = rs.getBytes(1); |
|
128 |
geom = parser.parse(data); |
|
129 |
|
|
130 |
|
|
131 |
} catch (SQLException e) { |
|
132 |
e.printStackTrace(); |
|
133 |
} |
|
134 |
|
|
135 |
return geom; |
|
136 |
} |
|
137 |
/** |
|
138 |
* @param conn |
|
139 |
* @param tableName |
|
140 |
* @param fields OJO: EL PRIMER CAMPO HA DE SER EL DE GEOMETRIA |
|
141 |
* @param whereClause |
|
142 |
*/ |
|
143 |
public void setData(Connection conn, String tableName, String fields, String whereClause, int id_FID_field) |
|
144 |
{ |
|
145 |
this.conn = conn; |
|
146 |
this.tableName = tableName; |
|
147 |
this.fields = fields; |
|
148 |
this.whereClause = whereClause; |
|
149 |
this.sqlOrig = "SELECT " + fields + " FROM " + tableName + " " + whereClause; |
|
150 |
this.idFID_FieldName = id_FID_field; |
|
151 |
try { |
|
152 |
st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); |
|
153 |
rs = st.executeQuery(sqlOrig); |
|
154 |
metaData = rs.getMetaData(); |
|
155 |
// Le pegamos un primera pasada para poder relacionar |
|
156 |
// un campo de identificador ?nico (parecido al OID en |
|
157 |
// postgresql) con el ?ndice dentro del recordset. |
|
158 |
// Esto cuando haya ediciones, no es v?lido, y hay |
|
159 |
// que refrescarlo. |
|
160 |
doRelateID_FID(); |
|
161 |
|
|
162 |
} catch (SQLException e) { |
|
163 |
// TODO Auto-generated catch block |
|
164 |
e.printStackTrace(); |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent() |
|
170 |
*/ |
|
171 |
public Rectangle2D getFullExtent(){ |
|
172 |
if (fullExtent == null) |
|
173 |
{ |
|
174 |
try |
|
175 |
{ |
|
176 |
IFeatureIterator itGeom = getFeatureIterator("SELECT ASBINARY(ogc_geom) AS the_geom FROM " + tableName); |
|
177 |
IGeometry geom; |
|
178 |
int cont = 0; |
|
179 |
while (itGeom.hasNext()) |
|
180 |
{ |
|
181 |
geom = itGeom.next().getGeometry(); |
|
182 |
if (cont==0) |
|
183 |
fullExtent = geom.getBounds2D(); |
|
184 |
else |
|
185 |
fullExtent.add(geom.getBounds2D()); |
|
186 |
cont++; |
|
187 |
} |
|
188 |
} |
|
189 |
catch (SQLException e) |
|
190 |
{ |
|
191 |
System.err.println(e.getMessage()); |
|
192 |
} catch (DriverException e) { |
|
193 |
// TODO Auto-generated catch block |
|
194 |
e.printStackTrace(); |
|
195 |
} |
|
196 |
|
|
197 |
} |
|
198 |
return fullExtent; |
|
199 |
} |
|
200 |
/* (non-Javadoc) |
|
201 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String) |
|
202 |
*/ |
|
203 |
public IFeatureIterator getFeatureIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException { |
|
204 |
Statement st; |
|
205 |
MySqlFeatureIterator geomIterator = null; |
|
206 |
try { |
|
207 |
st = conn.createStatement(); |
|
208 |
// st.setFetchSize(2000); |
|
209 |
ResultSet rs = st.executeQuery(sql); |
|
210 |
geomIterator = new MySqlFeatureIterator(rs); |
|
211 |
} catch (SQLException e) { |
|
212 |
e.printStackTrace(); |
|
213 |
throw new com.iver.cit.gvsig.fmap.DriverException(e); |
|
214 |
} |
|
215 |
|
|
216 |
return geomIterator; |
|
217 |
} |
|
218 |
/* (non-Javadoc) |
|
219 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D) |
|
220 |
*/ |
|
221 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException { |
|
222 |
if (workingArea != null) |
|
223 |
r = r.createIntersection(workingArea); |
|
224 |
double xMin = r.getMinX(); |
|
225 |
double yMin = r.getMinY(); |
|
226 |
double xMax = r.getMaxX(); |
|
227 |
double yMax = r.getMaxY(); |
|
228 |
|
|
229 |
String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", " |
|
230 |
+ xMax + " " + yMin + ", " |
|
231 |
+ xMax + " " + yMax + ", " |
|
232 |
+ xMin + " " + yMax + ")', " |
|
233 |
+ strEPSG + ")"; |
|
234 |
String sqlAux; |
|
235 |
if (getWhereClause().startsWith("WHERE")) |
|
236 |
sqlAux = sqlOrig + " MBRIntersects(" + wktBox + ",ogc_geom);" ; |
|
237 |
else |
|
238 |
sqlAux = sqlOrig + "WHERE MBRIntersects(" + wktBox + ",ogc_geom);" ; |
|
239 |
|
|
240 |
|
|
241 |
return getFeatureIterator(sqlAux); |
|
242 |
} |
|
243 |
public void open() { |
|
244 |
// TODO Auto-generated method stub |
|
245 |
|
|
246 |
} |
|
247 |
/** |
|
248 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning() |
|
249 |
*/ |
|
250 |
public String getConnectionStringBeginning() { |
|
251 |
return "jdbc:mysql:"; |
|
252 |
} |
|
253 |
|
|
254 |
static{ |
|
255 |
try { |
|
256 |
Class.forName("com.mysql.jdbc.Driver"); |
|
257 |
} catch (ClassNotFoundException e) { |
|
258 |
throw new RuntimeException(e); |
|
259 |
} |
|
260 |
} |
|
261 |
|
|
262 |
/** |
|
263 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String) |
|
264 |
*/ |
|
265 |
public String getGeometryField(String fieldName) { |
|
266 |
return "ASBINARY(" + fieldName +")"; |
|
267 |
} |
|
268 |
/** |
|
269 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys() |
|
270 |
*/ |
|
271 |
public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException { |
|
272 |
return new int[]{idFID_FieldName - 2}; |
|
273 |
} |
|
274 |
/** |
|
275 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort() |
|
276 |
*/ |
|
277 |
public int getDefaultPort() { |
|
278 |
return 3306; |
|
279 |
} |
|
280 |
/** |
|
281 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setWorkingArea(java.awt.geom.Rectangle2D) |
|
282 |
*/ |
|
283 |
public void setWorkingArea(Rectangle2D rect) { |
|
284 |
this.workingArea = rect; |
|
285 |
} |
|
286 |
/** |
|
287 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getWorkingArea() |
|
288 |
*/ |
|
289 |
public Rectangle2D getWorkingArea() { |
|
290 |
return workingArea; |
|
291 |
} |
|
292 |
/** |
|
293 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare) |
|
294 |
*/ |
|
295 |
public void write(DataWare arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException { |
|
296 |
// TODO Auto-generated method stub |
|
297 |
|
|
298 |
} |
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
} |
|
0 | 303 |
trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/mysql/testMySQL.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 03-mar-2005 |
|
3 |
* |
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
5 |
* |
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
7 |
* |
|
8 |
* This program is free software; you can redistribute it and/or |
|
9 |
* modify it under the terms of the GNU General Public License |
|
10 |
* as published by the Free Software Foundation; either version 2 |
|
11 |
* of the License, or (at your option) any later version. |
|
12 |
* |
|
13 |
* This program is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with this program; if not, write to the Free Software |
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
* |
|
22 |
* For more information, contact: |
|
23 |
* |
|
24 |
* Generalitat Valenciana |
|
25 |
* Conselleria d'Infraestructures i Transport |
|
26 |
* Av. Blasco Ib??ez, 50 |
|
27 |
* 46010 VALENCIA |
|
28 |
* SPAIN |
|
29 |
* |
|
30 |
* +34 963862235 |
|
31 |
* gvsig@gva.es |
|
32 |
* www.gvsig.gva.es |
|
33 |
* |
|
34 |
* or |
|
35 |
* |
|
36 |
* IVER T.I. S.A |
|
37 |
* Salamanca 50 |
|
38 |
* 46005 Valencia |
|
39 |
* Spain |
|
40 |
* |
|
41 |
* +34 963163400 |
|
42 |
* dac@iver.es |
|
43 |
*/ |
|
44 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql; |
|
45 |
|
|
46 |
import java.sql.DriverManager; |
|
47 |
import java.sql.ResultSet; |
|
48 |
import java.sql.Statement; |
|
49 |
import java.util.Enumeration; |
|
50 |
|
|
51 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
52 |
import com.iver.cit.gvsig.fmap.drivers.WKTParser; |
|
53 |
|
|
54 |
|
|
55 |
/** |
|
56 |
* @author FJP |
|
57 |
* |
|
58 |
* TODO To change the template for this generated type comment go to |
|
59 |
* Window - Preferences - Java - Code Generation - Code and Comments |
|
60 |
*/ |
|
61 |
public class testMySQL { |
|
62 |
|
|
63 |
public static void main(String[] args) |
|
64 |
{ |
|
65 |
/* System.err.println("dburl has the following format:"); |
|
66 |
System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME"); |
|
67 |
System.err.println("tablename is 'jdbc_test' by default."); |
|
68 |
System.exit(1); */ |
|
69 |
|
|
70 |
|
|
71 |
// Tarda casi 28 segundos en recuperar el tema de provincias!! |
|
72 |
String dburl = "jdbc:mysql://localhost/test"; |
|
73 |
String dbuser = "root"; |
|
74 |
String dbpass = "aquilina"; |
|
75 |
|
|
76 |
String dbtable = "vias"; |
|
77 |
|
|
78 |
java.sql.Connection conn; |
|
79 |
try |
|
80 |
{ |
|
81 |
System.out.println("Creating JDBC connection..."); |
|
82 |
Class.forName("com.mysql.jdbc.Driver"); |
|
83 |
Enumeration enumDrivers = DriverManager.getDrivers(); |
|
84 |
while (enumDrivers.hasMoreElements()) |
|
85 |
{ |
|
86 |
System.out.println("Driver " + enumDrivers.nextElement().toString()); |
|
87 |
} |
|
88 |
conn = DriverManager.getConnection(dburl, dbuser, dbpass); |
|
89 |
// magic trickery to be pgjdbc 7.2 compatible |
|
90 |
// This works due to the late binding of data types in most java VMs. As |
|
91 |
// this is more a demo source than a real-world app, we can risk this |
|
92 |
// problem. |
|
93 |
/* if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) { |
|
94 |
((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry"); |
|
95 |
((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d"); |
|
96 |
} else { |
|
97 |
((org.postgresql.PGConnection) conn).addDataType("geometry", "org.postgis.PGgeometry"); |
|
98 |
((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d"); |
|
99 |
} */ |
|
100 |
|
|
101 |
conn.setAutoCommit(false); |
|
102 |
|
|
103 |
/* |
|
104 |
* Create a statement and execute a select query. |
|
105 |
*/ |
|
106 |
// String strSQL = "select ogc_geom as geom from vias"; |
|
107 |
String strSQL = "select ASTEXT(ogc_geom) as geom from " + dbtable; |
|
108 |
/* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,"; |
|
109 |
strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias"; |
|
110 |
strSQL = strSQL + " WHERE TRUE"; |
|
111 |
*/ |
|
112 |
// PreparedStatement s = conn.prepareStatement(strSQL); |
|
113 |
long t1 = System.currentTimeMillis(); |
|
114 |
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
115 |
s.setFetchSize(2000); |
|
116 |
ResultSet r = s.executeQuery(strSQL); |
|
117 |
long t2 = System.currentTimeMillis(); |
|
118 |
|
|
119 |
System.out.println("Tiempo de consulta:" + (t2 - t1) + " milisegundos"); |
|
120 |
t1 = System.currentTimeMillis(); |
|
121 |
int cont = 0; |
|
122 |
WKTParser parser = new WKTParser(); |
|
123 |
while( r.next() ) |
|
124 |
{ |
|
125 |
/* |
|
126 |
* Retrieve the geometry as an object then cast it to the geometry type. |
|
127 |
* Print things out. |
|
128 |
*/ |
|
129 |
// Object obj = r.getObject(1); |
|
130 |
// InputStream inS = r.getAsciiStream(1); |
|
131 |
String strAux = r.getString(1); |
|
132 |
// IGeometry geom = parser.read(strAux); |
|
133 |
cont++; |
|
134 |
// int id = r.getInt(2); |
|
135 |
// System.out.println("Row " + id + ":"); |
|
136 |
// Geometry regeom = PGgeometry.geomFromString(obj.toString()); |
|
137 |
// System.out.println(obj.toString()); |
|
138 |
|
|
139 |
// PGgeometry geom = (PGgeometry)obj; |
|
140 |
/* int id = r.getInt(2); |
|
141 |
System.out.println("Row " + id + ":"); |
|
142 |
System.out.println(geom.toString()); */ |
|
143 |
} |
|
144 |
s.close(); |
|
145 |
conn.close(); |
|
146 |
t2 = System.currentTimeMillis(); |
|
147 |
|
|
148 |
System.out.println("Tiempo de recorrido:" + (t2 - t1) + " milisegundos. " + cont + " registros."); |
|
149 |
|
|
150 |
} |
|
151 |
catch( Exception e ) |
|
152 |
{ |
|
153 |
e.printStackTrace(); |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
0 | 157 |
trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/mysql/MySqlFeatureIterator.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 11-mar-2005 |
|
3 |
* |
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
5 |
* |
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
7 |
* |
|
8 |
* This program is free software; you can redistribute it and/or |
|
9 |
* modify it under the terms of the GNU General Public License |
|
10 |
* as published by the Free Software Foundation; either version 2 |
|
11 |
* of the License, or (at your option) any later version. |
|
12 |
* |
|
13 |
* This program is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with this program; if not, write to the Free Software |
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
* |
|
22 |
* For more information, contact: |
|
23 |
* |
|
24 |
* Generalitat Valenciana |
|
25 |
* Conselleria d'Infraestructures i Transport |
|
26 |
* Av. Blasco Ib??ez, 50 |
|
27 |
* 46010 VALENCIA |
|
28 |
* SPAIN |
|
29 |
* |
|
30 |
* +34 963862235 |
|
31 |
* gvsig@gva.es |
|
32 |
* www.gvsig.gva.es |
|
33 |
* |
|
34 |
* or |
|
35 |
* |
|
36 |
* IVER T.I. S.A |
|
37 |
* Salamanca 50 |
|
38 |
* 46005 Valencia |
|
39 |
* Spain |
|
40 |
* |
|
41 |
* +34 963163400 |
|
42 |
* dac@iver.es |
|
43 |
*/ |
|
44 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql; |
|
45 |
|
|
46 |
import java.sql.ResultSet; |
|
47 |
import java.sql.ResultSetMetaData; |
|
48 |
import java.sql.SQLException; |
|
49 |
import java.sql.Types; |
|
50 |
|
|
51 |
import com.hardcode.gdbms.engine.values.Value; |
|
52 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
|
53 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
|
54 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
|
55 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
56 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
|
57 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser; |
|
58 |
|
|
59 |
/** |
|
60 |
* @author FJP |
|
61 |
* |
|
62 |
* TODO To change the template for this generated type comment go to |
|
63 |
* Window - Preferences - Java - Code Generation - Code and Comments |
|
64 |
*/ |
|
65 |
public class MySqlFeatureIterator implements IFeatureIterator { |
|
66 |
private WKBParser parser = new WKBParser(); |
|
67 |
ResultSet rs; |
|
68 |
String strAux; |
|
69 |
IGeometry geom; |
|
70 |
int numColumns; |
|
71 |
private ResultSetMetaData metaData = null; |
|
72 |
Value[] regAtt; |
|
73 |
/** |
|
74 |
* @throws SQLException |
|
75 |
* |
|
76 |
*/ |
|
77 |
public MySqlFeatureIterator(ResultSet rs) { |
|
78 |
// Debe ser forward only |
|
79 |
this.rs = rs; |
|
80 |
try { |
|
81 |
numColumns = rs.getMetaData().getColumnCount(); |
|
82 |
regAtt = new Value[numColumns-1]; |
|
83 |
metaData = rs.getMetaData(); |
|
84 |
} catch (SQLException e) { |
|
85 |
// TODO Auto-generated catch block |
|
86 |
e.printStackTrace(); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
/* (non-Javadoc) |
|
91 |
* @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext() |
|
92 |
*/ |
|
93 |
public boolean hasNext() throws SQLException { |
|
94 |
if (rs.next()) |
|
95 |
return true; |
|
96 |
else |
|
97 |
{ |
|
98 |
rs.close(); |
|
99 |
return false; |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
/* (non-Javadoc) |
|
104 |
* @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next() |
|
105 |
*/ |
|
106 |
public IFeature next() throws SQLException { |
|
107 |
byte[] data = rs.getBytes(1); |
|
108 |
geom = parser.parse(data); |
|
109 |
for (int fieldId=2; fieldId <= numColumns; fieldId++ ) |
|
110 |
{ |
|
111 |
if (metaData.getColumnType(fieldId) == Types.VARCHAR) |
|
112 |
{ |
|
113 |
String strAux = rs.getString(fieldId); |
|
114 |
if (strAux == null) strAux = ""; |
|
115 |
regAtt[fieldId-2] = ValueFactory.createValue(strAux); |
|
116 |
} |
|
117 |
if (metaData.getColumnType(fieldId) == Types.FLOAT) |
|
118 |
regAtt[fieldId-2] = ValueFactory.createValue(rs.getFloat(fieldId)); |
|
119 |
if (metaData.getColumnType(fieldId) == Types.DOUBLE) |
|
120 |
regAtt[fieldId-2] = ValueFactory.createValue(rs.getDouble(fieldId)); |
|
121 |
if (metaData.getColumnType(fieldId) == Types.INTEGER) |
|
122 |
regAtt[fieldId-2] = ValueFactory.createValue(rs.getInt(fieldId)); |
|
123 |
if (metaData.getColumnType(fieldId) == Types.BIGINT) |
|
124 |
regAtt[fieldId-2] = ValueFactory.createValue(rs.getLong(fieldId)); |
|
125 |
if (metaData.getColumnType(fieldId) == Types.BIT) |
|
126 |
regAtt[fieldId-2] = ValueFactory.createValue(rs.getBoolean(fieldId)); |
|
127 |
if (metaData.getColumnType(fieldId) == Types.DATE) |
|
128 |
regAtt[fieldId-2] = ValueFactory.createValue(rs.getDate(fieldId)); |
|
129 |
|
|
130 |
} |
|
131 |
|
|
132 |
// TODO: Aqu? habr?a que usar una Factor?a. |
|
133 |
|
|
134 |
IFeature feat = new DefaultFeature(geom, regAtt); |
|
135 |
|
|
136 |
return feat; |
|
137 |
} |
|
138 |
|
|
139 |
} |
|
0 | 140 |
trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/postgis/PostGisFeatureIterator.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 11-mar-2005 |
|
3 |
* |
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
5 |
* |
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
7 |
* |
|
8 |
* This program is free software; you can redistribute it and/or |
|
9 |
* modify it under the terms of the GNU General Public License |
|
10 |
* as published by the Free Software Foundation; either version 2 |
|
11 |
* of the License, or (at your option) any later version. |
|
12 |
* |
|
13 |
* This program is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with this program; if not, write to the Free Software |
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
* |
|
22 |
* For more information, contact: |
|
23 |
* |
|
24 |
* Generalitat Valenciana |
|
25 |
* Conselleria d'Infraestructures i Transport |
|
26 |
* Av. Blasco Ib??ez, 50 |
|
27 |
* 46010 VALENCIA |
|
28 |
* SPAIN |
|
29 |
* |
|
30 |
* +34 963862235 |
|
31 |
* gvsig@gva.es |
|
32 |
* www.gvsig.gva.es |
|
33 |
* |
|
34 |
* or |
|
35 |
* |
|
36 |
* IVER T.I. S.A |
|
37 |
* Salamanca 50 |
|
38 |
* 46005 Valencia |
|
39 |
* Spain |
|
40 |
* |
|
41 |
* +34 963163400 |
|
42 |
* dac@iver.es |
|
43 |
*/ |
|
44 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.postgis; |
|
45 |
|
|
46 |
import java.nio.ByteBuffer; |
|
47 |
import java.sql.ResultSet; |
|
48 |
import java.sql.ResultSetMetaData; |
|
49 |
import java.sql.SQLException; |
|
50 |
import java.sql.Statement; |
|
51 |
import java.sql.Types; |
|
52 |
|
|
53 |
import com.hardcode.gdbms.engine.values.Value; |
|
54 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
|
55 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
|
56 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
|
57 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
58 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
|
59 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser; |
|
60 |
|
|
61 |
/** |
Also available in: Unified diff