io.h

00001 /**********************************************************************
00002  * $Id: io.h,v 1.5.2.1.2.1 2005/11/29 16:58:17 strk Exp $
00003  *
00004  * GEOS - Geometry Engine Open Source
00005  * http://geos.refractions.net
00006  *
00007  * Copyright (C) 2001-2002 Vivid Solutions Inc.
00008  * Copyright (C) 2005 Refractions Research Inc.
00009  *
00010  * This is free software; you can redistribute and/or modify it under
00011  * the terms of the GNU Lesser General Public Licence as published
00012  * by the Free Software Foundation. 
00013  * See the COPYING file for more information.
00014  *
00015  **********************************************************************/
00016 
00017 #ifndef GEOS_IO_H
00018 #define GEOS_IO_H
00019 
00020 #include <memory>
00021 #include <iostream>
00022 #include <string>
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         //WKTReader();
00077 
00086         WKTReader(const GeometryFactory *gf);
00087 
00088         ~WKTReader();
00089 
00091         Geometry* read(string wellKnownText);
00092 
00093 //      Geometry* read(Reader reader);  //Not implemented yet
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         //string(count, ch) can be used for this
00155         //static string stringOfChar(char ch, int count);
00156 
00158         string write(const Geometry *geometry);
00159 
00160         // Send Geometry's WKT to the given Writer
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 //      static const int INDENT = 2;
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  * \class ByteOrderValues io.h geos.h
00201  * 
00202  * Methods to read and write primitive datatypes from/to byte
00203  * sequences, allowing the byte order to be specified
00204  * 
00205  * Similar to the standard Java <code>ByteBuffer</code> class.
00206  */
00207 class ByteOrderValues {
00208 
00209 public:
00210         /*final*/ static int ENDIAN_BIG;
00211         /*final*/ 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  * \class ByteOrderDataInStream io.h geos.h
00226  * 
00227  * Allows reading an stream of primitive datatypes from an underlying
00228  * istream, with the representation being in either common byte ordering.
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() // throws ParseException
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() // throws ParseException
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() // throws ParseException
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() // throws ParseException
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         // buffers to hold primitive datatypes
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                 // throws IOException, ParseException
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         // for now support the WKB standard only - may be generalized later
00352         unsigned int inputDimension;
00353 
00354         ByteOrderDataInStream dis;
00355 
00356         vector<double> ordValues;
00357 
00358         Geometry *readGeometry();
00359                 // throws IOException, ParseException
00360 
00361         Point *readPoint();
00362                 // throws IOException
00363 
00364         LineString *readLineString();
00365                 // throws IOException
00366 
00367         LinearRing *readLinearRing();
00368                 // throws IOException
00369 
00370         Polygon *readPolygon();
00371                 // throws IOException
00372 
00373         MultiPoint *readMultiPoint();
00374                 // throws IOException, ParseException
00375 
00376         MultiLineString *readMultiLineString();
00377                 // throws IOException, ParseException
00378 
00379         MultiPolygon *readMultiPolygon();
00380                 // throws IOException, ParseException
00381 
00382         GeometryCollection *readGeometryCollection();
00383                 // throws IOException, ParseException
00384 
00385         CoordinateSequence *readCoordinateSequence(int); // throws IOException
00386 
00387         void readCoordinate(); // throws IOException
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                 // throws IOException, ParseException
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                 // throws IOException
00442 
00443         void writeLineString(const LineString &ls);
00444                 // throws IOException
00445 
00446         void writePolygon(const Polygon &p);
00447                 // throws IOException
00448 
00449         void writeGeometryCollection(const GeometryCollection &c, int wkbtype);
00450                 // throws IOException, ParseException
00451 
00452         void writeCoordinateSequence(const CoordinateSequence &cs, bool sized);
00453                 // throws IOException
00454 
00455         void writeCoordinate(const CoordinateSequence &cs, int idx, bool is3d);
00456                 // throws IOException
00457 
00458         void writeGeometryType(int geometryType);
00459                 // throws IOException
00460 
00461         void writeByteOrder();
00462                 // throws IOException
00463 
00464         void writeInt(int intValue);
00465                 // throws IOException
00466 
00467 };
00468 
00469 } // namespace geos
00470 
00471 #endif
00472 
00473 /**********************************************************************
00474  * $Log: io.h,v $
00475  * Revision 1.5.2.1.2.1  2005/11/29 16:58:17  strk
00476  * Back-ported WKB IO and C api.
00477  * Added required higher dimensional interfaces for CoordinateSequence
00478  *
00479  * Revision 1.5.2.1  2005/05/23 18:16:40  strk
00480  * more math.h to cmath conversions
00481  *
00482  * Revision 1.5  2004/07/19 10:33:12  strk
00483  * Class documentation changed to report geos.h as WKT writer/parser header file
00484  *
00485  * Revision 1.4  2004/07/08 19:34:49  strk
00486  * Mirrored JTS interface of CoordinateSequence, factory and
00487  * default implementations.
00488  * Added DefaultCoordinateSequenceFactory::instance() function.
00489  *
00490  * Revision 1.3  2004/07/07 10:29:54  strk
00491  * Adjusted exceptions documentation.
00492  *
00493  * Revision 1.2  2004/07/07 09:38:12  strk
00494  * Dropped WKTWriter::stringOfChars (implemented by std::string).
00495  * Dropped WKTWriter default constructor (internally created GeometryFactory).
00496  * Updated XMLTester to respect the changes.
00497  * Main documentation page made nicer.
00498  *
00499  * Revision 1.1  2004/07/02 13:20:42  strk
00500  * Header files moved under geos/ dir.
00501  *
00502  * Revision 1.15  2004/07/01 14:12:44  strk
00503  *
00504  * Geometry constructors come now in two flavors:
00505  *      - deep-copy args (pass-by-reference)
00506  *      - take-ownership of args (pass-by-pointer)
00507  * Same functionality is available through GeometryFactory,
00508  * including buildGeometry().
00509  *
00510  * Revision 1.14  2004/03/18 10:42:44  ybychkov
00511  * "IO" and "Util" upgraded to JTS 1.4
00512  * "Geometry" partially upgraded.
00513  *
00514  * Revision 1.13  2003/11/07 01:23:42  pramsey
00515  * Add standard CVS headers licence notices and copyrights to all cpp and h
00516  * files.
00517  *
00518  *
00519  **********************************************************************/
00520 

Generated on Mon Aug 6 22:04:47 2007 for GEOS by  doxygen 1.4.7