00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef GEOS_IO_H
00018 #define GEOS_IO_H
00019
00020 #include <memory>
00021 #include <iostream>
00022 #include <cstring>
00023 #include <geos/platform.h>
00024 #include <geos/geom.h>
00025 #include <geos/util.h>
00026 #include <iostream>
00027 #include <iomanip>
00028
00029 using namespace std;
00030
00031 namespace geos {
00032
00034 typedef unsigned char byte;
00035
00040 class ParseException: public GEOSException {
00041 public:
00042 ParseException();
00043 ParseException(string msg);
00044 ParseException(string msg, string var);
00045 ParseException(string msg, double num);
00046 ~ParseException();
00047 };
00048
00049 class StringTokenizer {
00050 public:
00051 enum {
00052 TT_EOF,
00053 TT_EOL,
00054 TT_NUMBER,
00055 TT_WORD
00056 };
00057 StringTokenizer();
00058 StringTokenizer(string txt);
00059 ~StringTokenizer();
00060 int nextToken();
00061 int peekNextToken();
00062 double getNVal();
00063 string getSVal();
00064 private:
00065 string str;
00066 string stok;
00067 double ntok;
00068 };
00069
00074 class WKTReader {
00075 public:
00076
00077
00086 WKTReader(const GeometryFactory *gf);
00087
00088 ~WKTReader();
00089
00091 Geometry* read(string wellKnownText);
00092
00093
00094
00095 protected:
00096 CoordinateSequence* getCoordinates(StringTokenizer *tokenizer);
00097 double getNextNumber(StringTokenizer *tokenizer);
00098 string getNextEmptyOrOpener(StringTokenizer *tokenizer);
00099 string getNextCloserOrComma(StringTokenizer *tokenizer);
00100 string getNextCloser(StringTokenizer *tokenizer);
00101 string getNextWord(StringTokenizer *tokenizer);
00102 Geometry* readGeometryTaggedText(StringTokenizer *tokenizer);
00103 Point* readPointText(StringTokenizer *tokenizer);
00104 LineString* readLineStringText(StringTokenizer *tokenizer);
00105 LinearRing* readLinearRingText(StringTokenizer *tokenizer);
00106 MultiPoint* readMultiPointText(StringTokenizer *tokenizer);
00107 Polygon* readPolygonText(StringTokenizer *tokenizer);
00108 MultiLineString* readMultiLineStringText(StringTokenizer *tokenizer);
00109 MultiPolygon* readMultiPolygonText(StringTokenizer *tokenizer);
00110 GeometryCollection* readGeometryCollectionText(StringTokenizer *tokenizer);
00111 private:
00112 const GeometryFactory *geometryFactory;
00113 const PrecisionModel *precisionModel;
00114 Coordinate* getPreciseCoordinate(StringTokenizer *tokenizer);
00115 bool isNumberNext(StringTokenizer *tokenizer);
00116 };
00117
00118 class Writer {
00119 public:
00120 Writer();
00121 ~Writer();
00122 void write(string txt);
00123 string toString();
00124 private:
00125 string str;
00126 };
00127
00149 class WKTWriter {
00150 public:
00151 WKTWriter();
00152 ~WKTWriter();
00153
00154
00155
00156
00158 string write(const Geometry *geometry);
00159
00160
00161 void write(const Geometry *geometry, Writer *writer);
00162
00163 string writeFormatted(const Geometry *geometry);
00164
00165 void writeFormatted(const Geometry *geometry, Writer *writer);
00166
00167 protected:
00168 string formatter;
00169 void appendGeometryTaggedText(const Geometry *geometry, int level, Writer *writer);
00170 void appendPointTaggedText(const Coordinate* coordinate, int level, Writer *writer, const PrecisionModel* precisionModel);
00171 void appendLineStringTaggedText(const LineString *lineString, int level, Writer *writer);
00172 void appendLinearRingTaggedText(const LinearRing *lineString, int level, Writer *writer);
00173 void appendPolygonTaggedText(const Polygon *polygon, int level, Writer *writer);
00174 void appendMultiPointTaggedText(const MultiPoint *multipoint, int level, Writer *writer);
00175 void appendMultiLineStringTaggedText(const MultiLineString *multiLineString, int level,Writer *writer);
00176 void appendMultiPolygonTaggedText(const MultiPolygon *multiPolygon, int level, Writer *writer);
00177 void appendGeometryCollectionTaggedText(const GeometryCollection *geometryCollection, int level,Writer *writer);
00178 void appendPointText(const Coordinate* coordinate, int level, Writer *writer, const PrecisionModel* precisionModel);
00179 void appendCoordinate(const Coordinate* coordinate, Writer *writer, const PrecisionModel* precisionModel);
00180 string writeNumber(double d);
00181 void appendLineStringText(const LineString *lineString, int level, bool doIndent, Writer *writer);
00182 void appendPolygonText(const Polygon *polygon, int level, bool indentFirst, Writer *writer);
00183 void appendMultiPointText(const MultiPoint *multiPoint, int level, Writer *writer);
00184 void appendMultiLineStringText(const MultiLineString *multiLineString, int level, bool indentFirst,Writer *writer);
00185 void appendMultiPolygonText(const MultiPolygon *multiPolygon, int level, Writer *writer);
00186 void appendGeometryCollectionText(const GeometryCollection *geometryCollection, int level,Writer *writer);
00187 private:
00188 enum {
00189 INDENT = 2
00190 };
00191
00192 static string createFormatter(const PrecisionModel* precisionModel);
00193 bool isFormatted;
00194 int level;
00195 void writeFormatted(const Geometry *geometry, bool isFormatted, Writer *writer);
00196 void indent(int level, Writer *writer);
00197 };
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 class ByteOrderValues {
00208
00209 public:
00210 static int ENDIAN_BIG;
00211 static int ENDIAN_LITTLE;
00212
00213 static int getInt(const byte *buf, int byteOrder);
00214 static void putInt(int intValue, byte *buf, int byteOrder);
00215
00216 static int64 getLong(const byte *buf, int byteOrder);
00217 static void putLong(int64 longValue, byte *buf, int byteOrder);
00218
00219 static double getDouble(const byte *buf, int byteOrder);
00220 static void putDouble(double doubleValue, byte *buf, int byteOrder);
00221
00222 };
00223
00224
00225
00226
00227
00228
00229
00230
00231 class ByteOrderDataInStream {
00232
00233 public:
00234
00235 ByteOrderDataInStream(istream *s=NULL):
00236 byteOrder(getMachineByteOrder()),
00237 stream(s) {}
00238 ~ByteOrderDataInStream() {}
00239
00244 void setInStream(istream *s) { stream=s; }
00245 void setOrder(int order) { byteOrder=order; }
00246
00247 byte readByte()
00248 {
00249 stream->read(reinterpret_cast<char *>(buf), 1);
00250 if ( stream->eof() )
00251 throw new ParseException("Unespected EOF parsing WKB");
00252 return buf[0];
00253 }
00254
00255 int readInt()
00256 {
00257 stream->read(reinterpret_cast<char *>(buf), 4);
00258 if ( stream->eof() )
00259 throw new ParseException("Unespected EOF parsing WKB");
00260 return ByteOrderValues::getInt(buf, byteOrder);
00261 }
00262
00263 long readLong()
00264 {
00265 stream->read(reinterpret_cast<char *>(buf), 8);
00266 if ( stream->eof() )
00267 throw new ParseException("Unespected EOF parsing WKB");
00268 return ByteOrderValues::getLong(buf, byteOrder);
00269 }
00270
00271 double readDouble()
00272 {
00273 stream->read(reinterpret_cast<char *>(buf), 8);
00274 if ( stream->eof() )
00275 throw new ParseException("Unespected EOF parsing WKB");
00276 return ByteOrderValues::getDouble(buf, byteOrder);
00277 }
00278
00279 private:
00280 int byteOrder;
00281 istream *stream;
00282
00283
00284 byte buf[8];
00285
00286 };
00287
00291 namespace WKBConstants {
00292 const int wkbXDR = 0;
00293 const int wkbNDR = 1;
00294 const int wkbPoint = 1;
00295 const int wkbLineString = 2;
00296 const int wkbPolygon = 3;
00297 const int wkbMultiPoint = 4;
00298 const int wkbMultiLineString = 5;
00299 const int wkbMultiPolygon = 6;
00300 const int wkbGeometryCollection = 7;
00301 }
00302
00303
00320 class WKBReader {
00321
00322 public:
00323
00324 WKBReader(const GeometryFactory &f): factory(f) {};
00325
00334 Geometry *read(istream &is);
00335
00336
00343 static ostream &printHEX(istream &is, ostream &os);
00344
00345 private:
00346
00347 static string BAD_GEOM_TYPE_MSG;
00348
00349 const GeometryFactory &factory;
00350
00351
00352 unsigned int inputDimension;
00353
00354 ByteOrderDataInStream dis;
00355
00356 vector<double> ordValues;
00357
00358 Geometry *readGeometry();
00359
00360
00361 Point *readPoint();
00362
00363
00364 LineString *readLineString();
00365
00366
00367 LinearRing *readLinearRing();
00368
00369
00370 Polygon *readPolygon();
00371
00372
00373 MultiPoint *readMultiPoint();
00374
00375
00376 MultiLineString *readMultiLineString();
00377
00378
00379 MultiPolygon *readMultiPolygon();
00380
00381
00382 GeometryCollection *readGeometryCollection();
00383
00384
00385 CoordinateSequence *readCoordinateSequence(int);
00386
00387 void readCoordinate();
00388
00389
00390 };
00391
00414 class WKBWriter {
00415
00416 public:
00417
00418 WKBWriter(int dims=2, int bo=getMachineByteOrder());
00419
00427 void write(const Geometry &g, ostream &os);
00428
00429
00430 private:
00431
00432 int outputDimension;
00433
00434 int byteOrder;
00435
00436 ostream *outStream;
00437
00438 byte buf[8];
00439
00440 void writePoint(const Point &p);
00441
00442
00443 void writeLineString(const LineString &ls);
00444
00445
00446 void writePolygon(const Polygon &p);
00447
00448
00449 void writeGeometryCollection(const GeometryCollection &c, int wkbtype);
00450
00451
00452 void writeCoordinateSequence(const CoordinateSequence &cs, bool sized);
00453
00454
00455 void writeCoordinate(const CoordinateSequence &cs, int idx, bool is3d);
00456
00457
00458 void writeGeometryType(int geometryType);
00459
00460
00461 void writeByteOrder();
00462
00463
00464 void writeInt(int intValue);
00465
00466
00467 };
00468
00469 }
00470
00471 #endif
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520