[geos-commits] r2998 - in trunk: include/geos/geom src/geom src/io tests/unit/geom

svn_geos at osgeo.org svn_geos at osgeo.org
Mon May 31 12:26:00 EDT 2010


Author: warmerdam
Date: 2010-05-31 12:25:57 -0400 (Mon, 31 May 2010)
New Revision: 2998

Modified:
   trunk/include/geos/geom/CoordinateArraySequence.h
   trunk/include/geos/geom/CoordinateArraySequenceFactory.h
   trunk/include/geos/geom/CoordinateSequenceFactory.h
   trunk/src/geom/CoordinateArraySequence.cpp
   trunk/src/geom/GeometryFactory.cpp
   trunk/src/io/WKTWriter.cpp
   trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp
Log:
Attempt to make geometry dimension perform more smoothly. 
Modified CoordinateArraySequence to default to unknown dimension (0), which
is determined at the point getDimension() is called by examination of the
first coordinate Z (ISNAN test).  The WKTWriter has also been altered to 
write 0.0 instead of nan, and to avoid writing "Z" for EMPTY geometries.(#348)


Modified: trunk/include/geos/geom/CoordinateArraySequence.h
===================================================================
--- trunk/include/geos/geom/CoordinateArraySequence.h	2010-05-31 02:57:51 UTC (rev 2997)
+++ trunk/include/geos/geom/CoordinateArraySequence.h	2010-05-31 16:25:57 UTC (rev 2998)
@@ -62,10 +62,10 @@
 
 	/// Construct sequence taking ownership of given Coordinate vector
 	CoordinateArraySequence(std::vector<Coordinate> *coords,
-                                std::size_t dimension = 3);
+                                std::size_t dimension = 0);
         
 	/// Construct sequence allocating space for n coordinates
-	CoordinateArraySequence(std::size_t n, std::size_t dimension = 3);
+	CoordinateArraySequence(std::size_t n, std::size_t dimension = 0);
 
 	~CoordinateArraySequence();
 
@@ -109,7 +109,7 @@
 
 	void expandEnvelope(Envelope &env) const;
 
-        std::size_t getDimension() const { return dimension; }
+    std::size_t getDimension() const;
 
 	void apply_rw(const CoordinateFilter *filter); 
 
@@ -119,7 +119,7 @@
 
 private:
 	std::vector<Coordinate> *vect;
-        std::size_t dimension;
+    mutable std::size_t dimension;
 };
 
 /// This is for backward API compatibility

Modified: trunk/include/geos/geom/CoordinateArraySequenceFactory.h
===================================================================
--- trunk/include/geos/geom/CoordinateArraySequenceFactory.h	2010-05-31 02:57:51 UTC (rev 2997)
+++ trunk/include/geos/geom/CoordinateArraySequenceFactory.h	2010-05-31 16:25:57 UTC (rev 2998)
@@ -54,7 +54,7 @@
 	CoordinateSequence *create(std::vector<Coordinate> *coords, std::size_t dims) const;
 
    	/** @see CoordinateSequenceFactory::create(std::size_t, int) */
-	CoordinateSequence *create(std::size_t size, std::size_t dimension=3) const;
+	CoordinateSequence *create(std::size_t size, std::size_t dimension=0) const;
 
 	/** \brief
 	 * Returns the singleton instance of CoordinateArraySequenceFactory

Modified: trunk/include/geos/geom/CoordinateSequenceFactory.h
===================================================================
--- trunk/include/geos/geom/CoordinateSequenceFactory.h	2010-05-31 02:57:51 UTC (rev 2997)
+++ trunk/include/geos/geom/CoordinateSequenceFactory.h	2010-05-31 16:25:57 UTC (rev 2998)
@@ -61,10 +61,11 @@
 	 * an empty sequence.
 	 *
 	 * @param coordinates the coordinates
+     * @param dimension 0, 2 or 3 with 0 indicating unknown at this time.
 	 */
 	virtual CoordinateSequence *create(
             std::vector<Coordinate> *coordinates,
-            std::size_t dimension=3 ) const=0;
+            std::size_t dimension=0 ) const=0;
 
 	/** \brief
 	 * Creates a CoordinateSequence of the specified size and dimension.
@@ -74,7 +75,7 @@
 	 *
 	 * @param size the number of coordinates in the sequence
 	 * @param dimension the dimension of the coordinates in the sequence
-	 * 	(if user-specifiable, otherwise ignored)
+	 * 	(0=unknown, 2, or 3 - ignored if not user specifiable)
 	 */
 	virtual CoordinateSequence *create(std::size_t size,
                                            std::size_t dimension) const=0;

Modified: trunk/src/geom/CoordinateArraySequence.cpp
===================================================================
--- trunk/src/geom/CoordinateArraySequence.cpp	2010-05-31 02:57:51 UTC (rev 2997)
+++ trunk/src/geom/CoordinateArraySequence.cpp	2010-05-31 16:25:57 UTC (rev 2998)
@@ -77,6 +77,23 @@
 	return vect; //new vector<Coordinate>(vect->begin(),vect->end());
 }
 
+std::size_t 
+CoordinateArraySequence::getDimension() const
+{
+    if( dimension != 0 )
+        return dimension;
+
+    if( vect->size() == 0 )
+        return 3;
+
+    if( ISNAN((*vect)[0].z) )
+        dimension = 2;
+    else
+        dimension = 3;
+
+    return dimension;
+}
+
 void
 CoordinateArraySequence::toVector(vector<Coordinate>& out) const
 {

Modified: trunk/src/geom/GeometryFactory.cpp
===================================================================
--- trunk/src/geom/GeometryFactory.cpp	2010-05-31 02:57:51 UTC (rev 2997)
+++ trunk/src/geom/GeometryFactory.cpp	2010-05-31 16:25:57 UTC (rev 2998)
@@ -193,7 +193,8 @@
 		coord.y = envelope->getMinY();
 		return createPoint(coord);
 	}
-	CoordinateSequence *cl=CoordinateArraySequenceFactory::instance()->create(NULL);
+	CoordinateSequence *cl=CoordinateArraySequenceFactory::instance()->
+        create((size_t) 0, 2);
 	coord.x = envelope->getMinX();
 	coord.y = envelope->getMinY();
 	cl->add(coord);

Modified: trunk/src/io/WKTWriter.cpp
===================================================================
--- trunk/src/io/WKTWriter.cpp	2010-05-31 02:57:51 UTC (rev 2997)
+++ trunk/src/io/WKTWriter.cpp	2010-05-31 16:25:57 UTC (rev 2998)
@@ -227,7 +227,7 @@
 		Writer *writer)
 {
 	writer->write("POINT ");
-    if( outputDimension == 3 && !old3D )
+    if( outputDimension == 3 && !old3D && coordinate != NULL )
         writer->write( "Z " );
 
 	appendPointText(coordinate, level, writer);
@@ -238,7 +238,7 @@
 		Writer *writer)
 {
 	writer->write("LINESTRING ");
-    if( outputDimension == 3 && !old3D )
+    if( outputDimension == 3 && !old3D && !lineString->isEmpty() )
         writer->write( "Z " );
 
 	appendLineStringText(lineString, level, false, writer);
@@ -253,42 +253,42 @@
  */
 void WKTWriter::appendLinearRingTaggedText(const LinearRing* linearRing, int level, Writer *writer) {
 	writer->write("LINEARRING ");
-    if( outputDimension == 3 && !old3D )
+    if( outputDimension == 3 && !old3D && !linearRing->isEmpty() )
         writer->write( "Z " );
 	appendLineStringText((LineString*)linearRing, level, false, writer);
 }
 
 void WKTWriter::appendPolygonTaggedText(const Polygon *polygon, int level, Writer *writer) {
 	writer->write("POLYGON ");
-    if( outputDimension == 3 && !old3D )
+    if( outputDimension == 3 && !old3D && !polygon->isEmpty())
         writer->write( "Z " );
 	appendPolygonText(polygon, level, false, writer);
 }
 
 void WKTWriter::appendMultiPointTaggedText(const MultiPoint *multipoint, int level, Writer *writer) {
 	writer->write("MULTIPOINT ");
-    if( outputDimension == 3 && !old3D )
+    if( outputDimension == 3 && !old3D && !multipoint->isEmpty() )
         writer->write( "Z " );
 	appendMultiPointText(multipoint, level, writer);
 }
 
 void WKTWriter::appendMultiLineStringTaggedText(const MultiLineString *multiLineString, int level,Writer *writer) {
 	writer->write("MULTILINESTRING ");
-    if( outputDimension == 3 && !old3D )
+    if( outputDimension == 3 && !old3D && !multiLineString->isEmpty() )
         writer->write( "Z " );
 	appendMultiLineStringText(multiLineString, level, false, writer);
 }
 
 void WKTWriter::appendMultiPolygonTaggedText(const MultiPolygon *multiPolygon, int level, Writer *writer) {
 	writer->write("MULTIPOLYGON ");
-    if( outputDimension == 3 && !old3D )
+    if( outputDimension == 3 && !old3D && !multiPolygon->isEmpty() )
         writer->write( "Z " );
 	appendMultiPolygonText(multiPolygon, level, writer);
 }
 
 void WKTWriter::appendGeometryCollectionTaggedText(const GeometryCollection *geometryCollection, int level,Writer *writer) {
 	writer->write("GEOMETRYCOLLECTION ");
-    if( outputDimension == 3 && !old3D ) // ? do we really do this?
+    if( outputDimension == 3 && !old3D && !geometryCollection->isEmpty() )
         writer->write( "Z " );
 	appendGeometryCollectionText(geometryCollection, level, writer);
 }
@@ -317,7 +317,10 @@
     if( outputDimension == 3 )
     {
         out+=" ";
-        out+=writeNumber(coordinate->z);
+        if( ISNAN(coordinate->z) )
+            out+=writeNumber(0.0);
+        else
+            out+=writeNumber(coordinate->z);
     }
 	writer->write(out);
 }

Modified: trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp
===================================================================
--- trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp	2010-05-31 02:57:51 UTC (rev 2997)
+++ trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp	2010-05-31 16:25:57 UTC (rev 2998)
@@ -68,7 +68,7 @@
 
 		ensure("Every coodinate in the default sequence should be same.", sequence.hasRepeatedPoints() );
 
-		const size_t dim = 3;
+		const size_t dim = 2; // default/empty coordinates now 2D.
 		ensure_equals( sequence.getDimension(), dim );
     }
 



More information about the geos-commits mailing list