/********************************************************************** * $Id: WKBReader.cpp,v 1.11.2.6 2006/05/02 14:04:10 strk Exp $ * * GEOS - Geometry Engine Open Source * http://geos.refractions.net * * Copyright (C) 2005 Refractions Research Inc. * * This is free software; you can redistribute and/or modify it under * the terms of the GNU Lesser General Public Licence as published * by the Free Software Foundation. * See the COPYING file for more information. * **********************************************************************/ #include //#define DEBUG_WKB_READER 1 namespace geos { string WKBReader::BAD_GEOM_TYPE_MSG = "bad geometry type encountered in "; ostream & WKBReader::printHEX(istream &is, ostream &os) { ios_base::fmtflags fl = os.flags(); // take note of output stream flags // Set hex,uppercase,fill and width output stream flags os.setf(ios::uppercase); os.setf(ios::hex, ios::basefield); os.fill('0'); long pos = is.tellg(); // take note of input stream get pointer is.seekg(0, ios::beg); // rewind input stream char each=0; while(is.read(&each, 1)) { os << setw(2) << static_cast(static_cast(each)); } is.clear(); // clear input stream eof flag is.seekg(pos); // reset input stream position os.setf(fl); // reset output stream status return os; } Geometry * WKBReader::read(istream &is) { dis.setInStream(&is); // will default to machine endian return readGeometry(); } Geometry * WKBReader::readGeometry() { // determine byte order byte byteOrder = dis.readByte(); #if DEBUG_WKB_READER cout<<"WKB byteOrder: "<<(int)byteOrder<setSRID(SRID); return result; } Point * WKBReader::readPoint() { readCoordinate(); #if DEBUG_WKB_READER cout<<"Coordinates: "<*holes=NULL; if ( numRings > 1 ) { try { holes = new vector(numRings-1); for (int i=0; isize(); i++) delete (*holes)[i]; delete holes; delete shell; throw; } } return factory.createPolygon(shell, holes); } MultiPoint * WKBReader::readMultiPoint() { int numGeoms = dis.readInt(); vector *geoms = new vector(numGeoms); try { for (int i=0; i(g)) throw new ParseException(BAD_GEOM_TYPE_MSG+ " MultiPoint"); (*geoms)[i] = g; } } catch (...) { for (unsigned int i=0; isize(); i++) delete (*geoms)[i]; delete geoms; throw; } return factory.createMultiPoint(geoms); } MultiLineString * WKBReader::readMultiLineString() { int numGeoms = dis.readInt(); vector *geoms = new vector(numGeoms); try { for (int i=0; i(g)) throw new ParseException(BAD_GEOM_TYPE_MSG+ " LineString"); (*geoms)[i] = g; } } catch (...) { for (unsigned int i=0; isize(); i++) delete (*geoms)[i]; delete geoms; throw; } return factory.createMultiLineString(geoms); } MultiPolygon * WKBReader::readMultiPolygon() { int numGeoms = dis.readInt(); vector *geoms = new vector(numGeoms); try { for (int i=0; i(g)) throw new ParseException(BAD_GEOM_TYPE_MSG+ " Polygon"); (*geoms)[i] = g; } } catch (...) { for (unsigned int i=0; isize(); i++) delete (*geoms)[i]; delete geoms; throw; } return factory.createMultiPolygon(geoms); } GeometryCollection * WKBReader::readGeometryCollection() { int numGeoms = dis.readInt(); vector *geoms = new vector(numGeoms); try { for (int i=0; isize(); i++) delete (*geoms)[i]; delete geoms; throw; } return factory.createGeometryCollection(geoms); } CoordinateSequence * WKBReader::readCoordinateSequence(int size) { CoordinateSequence *seq = factory.getCoordinateSequenceFactory()->create(size, inputDimension); unsigned int targetDim = seq->getDimension(); if ( targetDim > inputDimension ) targetDim = inputDimension; for (int i=0; isetOrdinate(i, j, ordValues[j]); } } return seq; } void WKBReader::readCoordinate() { static const PrecisionModel &pm = *factory.getPrecisionModel(); for (unsigned int i=0; i