[geos-commits] r3189 - in trunk: include/geos/geom/util tests/unit tests/unit/geom tests/unit/geom/util

svn_geos at osgeo.org svn_geos at osgeo.org
Mon Feb 7 12:52:11 EST 2011


Author: strk
Date: 2011-02-07 09:52:11 -0800 (Mon, 07 Feb 2011)
New Revision: 3189

Added:
   trunk/include/geos/geom/util/GeometryExtracter.h
   trunk/tests/unit/geom/util/
   trunk/tests/unit/geom/util/GeometryExtracterTest.cpp
Modified:
   trunk/include/geos/geom/util/Makefile.am
   trunk/tests/unit/Makefile.am
Log:
Port GeometryExtracter from JTS-1.12

Added: trunk/include/geos/geom/util/GeometryExtracter.h
===================================================================
--- trunk/include/geos/geom/util/GeometryExtracter.h	                        (rev 0)
+++ trunk/include/geos/geom/util/GeometryExtracter.h	2011-02-07 17:52:11 UTC (rev 3189)
@@ -0,0 +1,96 @@
+/**********************************************************************
+ * $Id$
+ *
+ * GEOS - Geometry Engine Open Source
+ * http://geos.refractions.net
+ *
+ * Copyright (C) 2011 Sandro Santilli <strk at keybit.net>
+ * Copyright (C) 2001-2002 Vivid Solutions Inc.
+ * Copyright (C) 2006 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.
+ *
+ ********************************************************************** 
+ *
+ * Last port: geom/util/GeometryExtracter.java r320 (JTS-1.12)
+ *
+ **********************************************************************/
+
+#ifndef GEOS_GEOM_UTIL_POINTEXTRACTER_H
+#define GEOS_GEOM_UTIL_POINTEXTRACTER_H
+
+#include <geos/export.h>
+#include <geos/geom/GeometryFilter.h>
+#include <geos/geom/GeometryCollection.h>
+#include <geos/platform.h>
+#include <vector>
+
+namespace geos {
+namespace geom { // geos.geom
+namespace util { // geos.geom.util
+
+/**
+ * Extracts the components of a given type from a {@link Geometry}.
+ */
+class GeometryExtracter {
+
+public:
+
+  /**
+   * Extracts the components of type <tt>clz</tt> from a {@link Geometry}
+   * and adds them to the provided container.
+   *
+   * @param geom the geometry from which to extract
+   * @param list the list to add the extracted elements to
+   */
+  template <class to, class container>
+  static void extract(const Geometry& geom, container& lst)
+  {
+    if ( const to* c = dynamic_cast<const to*>(&geom) )
+    {
+      lst.push_back(c);
+    }
+    else if ( const GeometryCollection* c =
+                   dynamic_cast<const GeometryCollection*>(&geom) )
+    {
+      GeometryExtracter::Extracter<to, container> extracter(lst);
+      c->apply_ro(&extracter);
+    }
+  }
+
+private:
+
+  template <class TO, class CO>
+  struct Extracter: public GeometryFilter {
+
+    /**
+     * Constructs a filter with a list in which to store the elements found.
+     *
+     * @param comps the container to extract into (will push_back to it)
+     */
+    Extracter(CO& comps) : _comps(comps) {}
+
+    CO& _comps;
+
+    void filter_ro(const Geometry* geom)
+    {
+      if ( const TO* c = dynamic_cast<const TO*>(geom) ) {
+        _comps.push_back(c);
+      }
+    }
+
+  };
+
+  // Declare type as noncopyable
+  GeometryExtracter(const GeometryExtracter& other);
+  GeometryExtracter& operator=(const GeometryExtracter& rhs);
+};
+
+} // namespace geos.geom.util
+} // namespace geos.geom
+} // namespace geos
+
+#endif

Modified: trunk/include/geos/geom/util/Makefile.am
===================================================================
--- trunk/include/geos/geom/util/Makefile.am	2011-02-07 17:00:45 UTC (rev 3188)
+++ trunk/include/geos/geom/util/Makefile.am	2011-02-07 17:52:11 UTC (rev 3189)
@@ -14,6 +14,7 @@
     GeometryCombiner.h \
     GeometryEditor.h \
     GeometryEditorOperation.h \
+    GeometryExtracter.h \
     GeometryTransformer.h \
     LinearComponentExtracter.h \
     PointExtracter.h \

Modified: trunk/tests/unit/Makefile.am
===================================================================
--- trunk/tests/unit/Makefile.am	2011-02-07 17:00:45 UTC (rev 3188)
+++ trunk/tests/unit/Makefile.am	2011-02-07 17:52:11 UTC (rev 3189)
@@ -59,6 +59,7 @@
 	geom/PolygonTest.cpp \
 	geom/prep/PreparedGeometryFactoryTest.cpp \
 	geom/TriangleTest.cpp \
+	geom/util/GeometryExtracterTest.cpp \
 	index/quadtree/DoubleBitsTest.cpp \
 	io/ByteOrderValuesTest.cpp \
 	io/WKBReaderTest.cpp \

Added: trunk/tests/unit/geom/util/GeometryExtracterTest.cpp
===================================================================
--- trunk/tests/unit/geom/util/GeometryExtracterTest.cpp	                        (rev 0)
+++ trunk/tests/unit/geom/util/GeometryExtracterTest.cpp	2011-02-07 17:52:11 UTC (rev 3189)
@@ -0,0 +1,99 @@
+// $Id$
+// 
+// Test Suite for geos::geom::util::GeometryExtracter class.
+
+// tut
+#include <tut.hpp>
+// geos
+#include <geos/platform.h>
+#include <geos/geom/PrecisionModel.h>
+#include <geos/geom/GeometryFactory.h>
+#include <geos/geom/Point.h>
+#include <geos/geom/LineString.h>
+#include <geos/geom/Polygon.h>
+#include <geos/io/WKTReader.h>
+#include <geos/io/WKTWriter.h>
+#include <geos/geom/util/GeometryExtracter.h>
+// std
+#include <vector>
+
+namespace tut
+{
+    //
+    // Test Group
+    //
+
+    // Common data used by tests
+    struct test_geometryextracter_data
+    {
+      geos::geom::PrecisionModel pm;
+      geos::geom::GeometryFactory gf;
+      geos::io::WKTReader wktreader;
+      geos::io::WKTWriter wktwriter;
+
+		  typedef std::auto_ptr<geos::geom::Geometry> GeomPtr;
+
+      typedef geos::io::WKTReader WKTReader;
+      typedef geos::io::WKTWriter WKTWriter;
+
+      test_geometryextracter_data()
+        :
+        pm(1.0),
+        gf(&pm),
+        wktreader(&gf)
+      {
+      }
+    };
+
+    typedef test_group<test_geometryextracter_data> group;
+    typedef group::object object;
+
+    group test_geometryextracter_group("geos::geom::util::GeometryExtracter");
+
+    //
+    // Test Cases
+    //
+
+    // Test extraction of single point
+    template<>
+    template<>
+    void object::test<1>()
+    {
+      using namespace geos::geom::util;
+      using namespace geos::geom;
+
+      GeomPtr geom(wktreader.read("POINT(-117 33)"));
+      std::vector<const Point*> lst_points;
+      std::vector<const LineString*> lst_lines;
+
+      GeometryExtracter::extract<Point>(*geom, lst_points);
+      GeometryExtracter::extract<LineString>(*geom, lst_lines);
+
+      ensure_equals( lst_points.size(), 1u );
+      ensure_equals( lst_lines.size(), 0u );
+    }
+
+    // Test extraction of multiple types 
+    template<>
+    template<>
+    void object::test<2>()
+    {
+      using namespace geos::geom::util;
+      using namespace geos::geom;
+
+      GeomPtr geom(wktreader.read("GEOMETRYCOLLECTION(POINT(-117 33),LINESTRING(0 0, 10 0),POINT(0 0),POLYGON((0 0, 10 0, 10 10, 0 10, 0 0)),LINESTRING(10 0, 23 30),POINT(20 20))"));
+
+      std::vector<const Point*> lst_points;
+      std::vector<const LineString*> lst_lines;
+      std::vector<const Polygon*> lst_polys;
+
+      GeometryExtracter::extract<Point>(*geom, lst_points);
+      GeometryExtracter::extract<LineString>(*geom, lst_lines);
+      GeometryExtracter::extract<Polygon>(*geom, lst_polys);
+
+      ensure_equals( lst_points.size(), 3u );
+      ensure_equals( lst_lines.size(), 2u );
+      ensure_equals( lst_polys.size(), 1u );
+    }
+
+} // namespace tut



More information about the geos-commits mailing list