Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57786 - in sandbox/ggl/other/comparisons/jts: . src src/main src/main/java src/main/java/com src/main/java/com/geodan src/main/java/com/geodan/geom
From: barend.gehrels_at_[hidden]
Date: 2009-11-19 12:11:41


Author: barendgehrels
Date: 2009-11-19 12:11:40 EST (Thu, 19 Nov 2009)
New Revision: 57786
URL: http://svn.boost.org/trac/boost/changeset/57786

Log:
Added JTS to benchmarks
Added:
   sandbox/ggl/other/comparisons/jts/
   sandbox/ggl/other/comparisons/jts/pom.xml (contents, props changed)
   sandbox/ggl/other/comparisons/jts/readme.txt (contents, props changed)
   sandbox/ggl/other/comparisons/jts/src/
   sandbox/ggl/other/comparisons/jts/src/main/
   sandbox/ggl/other/comparisons/jts/src/main/java/
   sandbox/ggl/other/comparisons/jts/src/main/java/com/
   sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/
   sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/geom/
   sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/geom/Compare.java (contents, props changed)
   sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/geom/GeomBenchmark.java (contents, props changed)

Added: sandbox/ggl/other/comparisons/jts/pom.xml
==============================================================================
--- (empty file)
+++ sandbox/ggl/other/comparisons/jts/pom.xml 2009-11-19 12:11:40 EST (Thu, 19 Nov 2009)
@@ -0,0 +1,146 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.geodan</groupId>
+ <artifactId>geom-performance-check</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <description/>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+
+ <!-- ==== Execute =============================== -->
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>java</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <executable>java</executable>
+ <classpathScope>test</classpathScope>
+ <arguments>
+ <argument>-classpath</argument>
+ <!-- automatically creates the classpath using all project dependencies,also adding the project build directory -->
+ <classpath/>
+ <argument>-Xmx512m</argument>
+ <argument>com.geodan.geom.GeomBenchmark</argument>
+ </arguments>
+ </configuration>
+ </plugin>
+
+ </plugins>
+
+ </build>
+
+ <!-- =========================================================== -->
+ <!-- Repositories (ibiblio, refractions...). -->
+ <!-- This is where Maven looks for dependencies. -->
+ <!-- =========================================================== -->
+
+ <repositories>
+ <repository>
+ <id>maven2-repository.dev.java.net</id>
+ <name>Java.net repository</name>
+ <url>http://download.java.net/maven/2>
+ </repository>
+
+ <repository>
+ <id>osgeo</id>
+ <name>Open Source Geospatial Foundation Repository</name>
+ <url>
http://download.osgeo.org/webdav/geotools/>
+ </repository>
+
+ <repository>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ <id>opengeo</id>
+ <name>OpenGeo Maven Repository</name>
+ <url>
http://repo.opengeo.org>
+ </repository>
+ </repositories>
+
+
+
+ <properties>
+ <spring.version>2.5.6</spring.version>
+ <geotools.version>2.6-M2</geotools.version>
+ </properties>
+
+ <dependencies>
+ <!--<dependency>
+ <groupId>com.vividsolutions</groupId>
+ <artifactId>jts</artifactId>
+ <version>1.9</version>
+ </dependency>
+
+ --><dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>${spring.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.4</version>
+ <scope>test</scope>
+ </dependency>
+
+
+ <dependency>
+ <groupId>org.geotools</groupId>
+ <artifactId>gt-main</artifactId>
+ <version>${geotools.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.geotools</groupId>
+ <artifactId>gt-shapefile</artifactId>
+ <version>${geotools.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.geotools</groupId>
+ <artifactId>gt-epsg-hsql</artifactId>
+ <version>${geotools.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.5.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>log4j-over-slf4j</artifactId>
+ <version>1.5.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>jcl-over-slf4j</artifactId>
+ <version>1.5.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>0.9.16</version>
+ </dependency>
+ </dependencies>
+
+</project>

Added: sandbox/ggl/other/comparisons/jts/readme.txt
==============================================================================
--- (empty file)
+++ sandbox/ggl/other/comparisons/jts/readme.txt 2009-11-19 12:11:40 EST (Thu, 19 Nov 2009)
@@ -0,0 +1,6 @@
+run bencmark using maven :
+
+mvn clean compile
+
+mvn exec:exec
+

Added: sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/geom/Compare.java
==============================================================================
--- (empty file)
+++ sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/geom/Compare.java 2009-11-19 12:11:40 EST (Thu, 19 Nov 2009)
@@ -0,0 +1,122 @@
+package com.geodan.geom;
+
+public class Compare {
+
+ public final static int AREA_COUNT = 10;
+
+ public final static double AREA_DIVISOR = 1.0;
+ public final static double LENGTH_DIVISOR = 1.0;
+
+ public final static double SIMPLIFY_DISTANCE = 0.0008975;
+ // results in simplified length of 99%, vertex reduction of ca. 10%
+
+ public final static int CENTROID_COUNT = 10;
+
+ public final static double CLIP_DISTANCE = 0.1;
+
+ public final static int OVERLAY_COUNT = 1;
+ public final static int CLIP_COUNT = 1;
+ public final static double INTEGER_FACTOR = 10000.0;
+
+ // #define OVERLAY_UNION
+
+ public final static int OVERLAY_ELLIPSE_COUNT = 101;// 101;
+ public final static double delta = Math.PI * 2.0/(OVERLAY_ELLIPSE_COUNT - 1);
+ public final static double OVERLAY_ELLIPSE_FACTOR1 = 1.1; // 1.1
+ public final static double OVERLAY_ELLIPSE_FACTOR2 = 0.2; // 0.2
+ public final static double CLIP_FACTOR = 0.9;
+
+ public final static boolean MEASURE_AREA = true;
+ public final static boolean MEASURE_CENTROID = true;
+ public final static boolean MEASURE_CLIP = true;
+ public final static boolean MEASURE_CONVEX_HULL = true;
+ public final static boolean MEASURE_OVERLAY = true; //effe uitezet ivm
+ public final static boolean MEASURE_SIMPLIFY = true;
+ public final static boolean MEASURE_TOUCH = true;
+// currently only for GEOS, not further worked out
+
+ public final static boolean MEASURE_CONTAINS = true; // jts ONLY so far
+ public final static boolean MEASURE_WITHIN = true;
+
+ // Variables to check the results
+ public final static boolean CLIP_AREA = true;
+ public final static boolean HULL_AREA = true;
+ public final static boolean OVERLAY_AREA = true;
+ public final static boolean SIMPLIFY_LENGTH = true;
+
+ public static void report_area(long l, int n, double area) {
+ area /= AREA_COUNT;
+ area /= AREA_DIVISOR;
+ report_common(l, n, "AREA", area, 0);
+ }
+
+ public static void report_centroid(long l, int n, double sumX, double sumY) {
+ sumX /= n * CENTROID_COUNT;
+ sumY /= n * CENTROID_COUNT;
+ report_common(l, n, "CENTROID", sumX, sumY);
+ }
+
+ public static void report_hull(long l, int n, double area) {
+ report_common(l, n, "HULL", area / AREA_DIVISOR, 0);
+ }
+
+ public static void report_clip(long l, int n, double area1, double area2) {
+ area1 /= CLIP_COUNT;
+ area2 /= CLIP_COUNT;
+ area1 /= AREA_DIVISOR;
+ area2 /= AREA_DIVISOR;
+ report_common(l, n, "CLIP", area2, area1);
+ }
+
+ public static void report_overlay(long l, int size, double area1,
+ double area2) {
+ area1 /= OVERLAY_COUNT;
+ area2 /= OVERLAY_COUNT;
+ area1 /= AREA_DIVISOR;
+ area2 /= AREA_DIVISOR;
+ report_common(l, OVERLAY_ELLIPSE_COUNT,
+ // #if defined(OVERLAY_UNION)
+ // "UNION"
+ // #elif defined(OVERLAY_SYMDIFF)
+ // "SYM_DIFFERENCE"
+ // #else
+ "INTERSECTION"
+ // #endif
+ , area2, area1);
+ }
+
+ public static void report_simplify(long l, int n, double length1,
+ double length2, int count1, int count2) {
+ length1 /= LENGTH_DIVISOR;
+ length2 /= LENGTH_DIVISOR;
+ report_common(l, n, "SIMPLIFY", length2, length1);
+ }
+
+ public static void report_within(long l, int size, int count,
+ int count_boundary) {
+ report_common(l, size, "WITHIN", count, count_boundary);
+ }
+
+ public static void report_contains(long l, int size, int count,
+ int count_boundary) {
+ report_common(l, size, "CONTAINS", count, count_boundary);
+ }
+
+ public static void report_touch(long l, int n, int count, int count_box) {
+ report_common(l, n, "TOUCH", count, count_box);
+ }
+
+ public static void report_common(double s, int n, String algorithm,
+ Object value1, Object value2) {
+ System.out.println(algorithm + " " + n + " total: " + value1
+ + " other: " + value2 + " time: " + s / 1.0e9 + " s");
+ System.gc();
+ try {
+ Thread.currentThread().sleep(100);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+}

Added: sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/geom/GeomBenchmark.java
==============================================================================
--- (empty file)
+++ sandbox/ggl/other/comparisons/jts/src/main/java/com/geodan/geom/GeomBenchmark.java 2009-11-19 12:11:40 EST (Thu, 19 Nov 2009)
@@ -0,0 +1,561 @@
+package com.geodan.geom;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.geotools.data.DataStore;
+import org.geotools.data.DataStoreFinder;
+import org.geotools.data.FeatureSource;
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureIterator;
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
+
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.CoordinateList;
+import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.GeometryFactory;
+import com.vividsolutions.jts.geom.LinearRing;
+import com.vividsolutions.jts.geom.MultiPolygon;
+import com.vividsolutions.jts.geom.Point;
+import com.vividsolutions.jts.geom.Polygon;
+import com.vividsolutions.jts.geom.prep.PreparedPolygon;
+import com.vividsolutions.jts.simplify.DouglasPeuckerSimplifier;
+
+public class GeomBenchmark {
+
+ private List<Polygon> polygons;
+ private List<Envelope> boxes;
+
+ private List<Envelope> clip_boxes; // for clips
+ private List<Polygon> ellipses; // for intersections
+ private List<Integer> ids; // for tracing
+
+ private List<PreparedPolygon> ppolygons;
+
+ private GeometryFactory geometryFactory;
+
+ // intersections/unions
+
+ public GeomBenchmark() {
+ super();
+ polygons = new ArrayList<Polygon>();
+ boxes = new ArrayList<Envelope>();
+ clip_boxes = new ArrayList<Envelope>(); // for clips
+ ellipses = new ArrayList<Polygon>(); // for
+
+ geometryFactory = new GeometryFactory();
+
+ ids = new ArrayList<Integer>();
+
+ }
+
+ public void prepare(File shapefile) {
+ polygons.clear();
+ boxes.clear();
+ clip_boxes.clear();
+ ellipses.clear();
+ ids.clear();
+
+ geometryFactory = new GeometryFactory();
+
+ try {
+ // load all shapes from shapefile into polygons
+ read_shapefile(shapefile, polygons, ids);
+
+ } catch (Exception e) {
+ System.out.println(e.getMessage());
+ System.exit(1);
+ }
+
+ // ////////////////////////////////////////////////////////////////
+ ppolygons = new ArrayList<PreparedPolygon>(polygons.size());
+
+ for (Polygon polygon : polygons) {
+ PreparedPolygon prepoly = new PreparedPolygon(polygon);
+ ppolygons.add(prepoly);
+ if (prepoly.contains(polygon.getCentroid())) {
+ // needed to cache it
+ }
+ }
+ // ////////////////////////////////////////////////////////////////
+
+ // Create envelopes
+ for (Polygon polygon : polygons) {
+ boxes.add(polygon.getEnvelopeInternal());
+ }
+ // Create the star-ellipses for intersections later on
+ if (Compare.MEASURE_OVERLAY || Compare.MEASURE_CLIP) {
+ int k = 0;
+ for (Envelope box : boxes) {
+ k++;
+
+ double cx = box.centre().x;
+ double cy = box.centre().y;
+
+ double dx = box.getWidth();
+ double dy = box.getHeight();
+
+ if (Compare.MEASURE_OVERLAY) {
+ double a1 = Compare.OVERLAY_ELLIPSE_FACTOR1 * 0.5 * dx;
+ double b1 = Compare.OVERLAY_ELLIPSE_FACTOR1 * 0.5 * dy;
+ double a2 = Compare.OVERLAY_ELLIPSE_FACTOR2 * 0.5 * dx;
+ double b2 = Compare.OVERLAY_ELLIPSE_FACTOR2 * 0.5 * dy;
+
+ // We will use a coordinate list to build the linearring
+ CoordinateList clist = new CoordinateList();
+ // Compare.OVERLAY_ELLIPSE_COUNT);
+ double angle = 0.0; // 45.0 * ggl::math::d2r; //0.0;
+ for (int i = 0; i < Compare.OVERLAY_ELLIPSE_COUNT - 1; i++, angle += Compare.delta) {
+ if (i % 2 == 0) {
+ clist.add(new Coordinate(cx + a1 * Math.sin(angle),
+ cy + b1 * Math.cos(angle)));
+ } else {
+ clist.add(new Coordinate(cx + a2 * Math.sin(angle),
+ cy + b2 * Math.cos(angle)));
+ }
+ }
+
+ clist.add(clist.get(0));
+ LinearRing lr = geometryFactory.createLinearRing(clist
+ .toCoordinateArray());
+ Polygon ellipse = geometryFactory.createPolygon(lr, null);
+ ellipses.add(ellipse);
+ }
+
+ if (Compare.MEASURE_CLIP) {
+
+ // note : weird use of sin/cos for a constant angle
+ // effectively this create a box . shrinkBy(
+ // Compare.CLIP_FACTOR * 0.5*sqrt2)?
+ double a = Compare.CLIP_FACTOR * 0.5 * dx;
+ double b = Compare.CLIP_FACTOR * 0.5 * dy;
+
+ double angle1 = Math.toRadians(225.0);
+ double angle2 = Math.toRadians(45.0);
+
+ double x0 = (cx + a * Math.sin(angle1));
+ double y0 = (cy + b * Math.cos(angle1));
+
+ double x1 = (cx + a * Math.sin(angle2));
+ double y1 = (cy + b * Math.cos(angle2));
+
+ Envelope clipbox = new Envelope(x0, x1, y0, y1);
+ clip_boxes.add(clipbox);
+ }
+ }
+ }
+ }
+
+ // sleep to allow buffers and malloc to settle
+ // Compare.wait();
+
+ public void runchecks() {
+ if (Compare.MEASURE_AREA) {
+ double area = 0;
+ long t0 = System.nanoTime();
+ for (int i = 0; i < Compare.AREA_COUNT; i++) {
+ for (Polygon polygon : polygons) {
+ area += polygon.getArea();
+ }
+ }
+ long t1 = System.nanoTime();
+ Compare.report_area(t1 - t0, polygons.size(), area);
+ }
+
+ if (Compare.MEASURE_CENTROID) {
+ double sum_x = 0, sum_y = 0;
+ long t0 = System.nanoTime();
+ for (int i = 0; i < Compare.CENTROID_COUNT; i++) {
+ for (Polygon polygon : polygons) {
+ Point centroid = polygon.getCentroid();
+ sum_x += centroid.getX();
+ sum_y += centroid.getY();
+ }
+ }
+ long t1 = System.nanoTime();
+ Compare.report_centroid(t1 - t0, polygons.size(), sum_x, sum_y);
+ }
+
+ if (Compare.MEASURE_CONVEX_HULL) {
+ double area = 0.0;
+ long t0 = System.nanoTime();
+ for (Polygon polygon : polygons) {
+ Geometry hull = polygon.convexHull();
+ if (Compare.HULL_AREA) {
+ area += Math.abs(hull.getArea());
+ }
+ }
+ long t1 = System.nanoTime();
+ Compare.report_hull(t1 - t0, polygons.size(), area);
+ }
+
+ if (Compare.MEASURE_OVERLAY) {
+ double area1 = 0.0, area2 = 0.0;
+ long t0 = System.nanoTime();
+ for (int i = 0; i < Compare.OVERLAY_COUNT; i++) {
+ // TODO : weird loop C++ port??
+ int k = 0;
+ Iterator<Polygon> eit = ellipses.iterator();
+ for (Iterator<Polygon> pit = polygons.iterator(); pit.hasNext()
+ && eit.hasNext(); k++) {
+ Polygon poly = pit.next();
+ Polygon ellipse = eit.next();
+ if (Compare.OVERLAY_AREA) {
+ area1 += poly.getArea();
+ }
+ Geometry v = ellipse.intersection(poly);
+ if (Compare.OVERLAY_AREA) {
+ area2 += v.getArea();
+ }
+ }
+ }
+ long t1 = System.nanoTime();
+ Compare.report_overlay(t1 - t0, polygons.size(), area1, area2);
+ }
+
+ if (Compare.MEASURE_CLIP) {
+ boolean first = true;
+ double area1 = 0.0, area2 = 0.0;
+
+ long t0 = System.nanoTime();
+
+ for (int i = 0; i < Compare.CLIP_COUNT; i++) {
+ Iterator<Envelope> bit = clip_boxes.iterator();
+ Iterator<Polygon> pit = polygons.iterator();
+ for (int k = 0; pit.hasNext() && bit.hasNext(); k++) {
+ Polygon poly = pit.next();
+ Envelope clipenv = bit.next();
+ Geometry clipgeom = geometryFactory.toGeometry(clipenv);
+ if (Compare.CLIP_AREA) {
+ area1 += poly.getArea();
+ }
+ Geometry v = clipgeom.intersection(poly);
+ if (Compare.CLIP_AREA) {
+ area2 += v.getArea();
+ }
+ }
+ }
+ long t1 = System.nanoTime();
+ Compare.report_clip(t1 - t0, polygons.size(), area1, area2);
+ }
+
+ if (Compare.MEASURE_SIMPLIFY) {
+ int count1 = 0, count2 = 0;
+ double length1 = 0.0, length2 = 0.0;
+ long t0 = System.nanoTime();
+ for (Polygon polygon : polygons) {
+ Geometry simplegeom = DouglasPeuckerSimplifier.simplify(
+ polygon, Compare.SIMPLIFY_DISTANCE);
+ count1 += polygon.getNumPoints();
+ count2 += simplegeom.getNumPoints();
+ if (Compare.SIMPLIFY_LENGTH) {
+ length1 += polygon.getLength();
+ length2 += simplegeom.getLength();
+ }
+
+ }
+ long t1 = System.nanoTime();
+ Compare.report_simplify(t1 - t0, polygons.size(), length1, length2,
+ count1, count2);
+ }
+
+ // nearly straight port from original
+ if (Compare.MEASURE_WITHIN) {
+ int count = 0;
+ long t0 = System.nanoTime();
+ for (int e = 0; e < boxes.size(); e++) {
+ Envelope b = boxes.get(e);
+ Coordinate c = b.centre();
+ // todo: expensive call in jts ! should not be in measurements ;
+ Point p = geometryFactory.createPoint(c);
+ // TODO : port to java: what is intended with this loop
+ // construct? it may be ported wrong
+ Iterator<Envelope> bit = boxes.iterator();
+ Iterator<Polygon> pit = polygons.iterator();
+ for (int k = 0; pit.hasNext() && bit.hasNext(); k++) {
+ Polygon poly = pit.next();
+ Envelope box = bit.next();
+ // todo: jts does not support coordinate.within(envelope);
+ // it is checked by p.within anyway so should be removed
+ // from the test??
+ if (box.contains(c) && p.within(poly)) {
+ count++;
+ }
+ }
+ }
+ long t1 = System.nanoTime();
+ Compare.report_within(t1 - t0, polygons.size(), count, -1);
+ }
+
+ // alternative port : calc points first, outside timer
+ if (Compare.MEASURE_WITHIN) {
+
+ int count = 0;
+ List<Point> points = new ArrayList<Point>(boxes.size());
+ for (int e = 0; e < boxes.size(); e++) {
+ Envelope b = boxes.get(e);
+ Coordinate c = b.centre();
+ Point p = geometryFactory.createPoint(c);
+ points.add(p);
+ }
+
+ long t0 = System.nanoTime();
+ Iterator<Point> pointIt = points.iterator();
+ Iterator<Polygon> pit = polygons.iterator();
+ Iterator<Envelope> bit = boxes.iterator();
+ for (int k = 0; pit.hasNext() && bit.hasNext() && pointIt.hasNext(); k++) {
+ Polygon poly = pit.next();
+ Envelope box = bit.next();
+ Point p = pointIt.next();
+ // todo: jts does not support coordinate.within(envelope);
+ // it is checked by p.within anyway so should be removed from
+ // the test??
+ if (box.contains(box.centre()) && p.within(poly)) {
+ count++;
+ }
+ }
+ long t1 = System.nanoTime();
+
+ System.out
+ .println("within alternative port : calc points first, outside timer");
+ Compare.report_within(t1 - t0, polygons.size(), count, -1);
+ }
+
+ // alternative port : calc points first, outside timer, skip boundingbox
+ if (Compare.MEASURE_WITHIN) {
+ int count = 0;
+ List<Point> points = new ArrayList<Point>(boxes.size());
+ for (int e = 0; e < boxes.size(); e++) {
+ Envelope b = boxes.get(e);
+ Coordinate c = b.centre();
+ Point p = geometryFactory.createPoint(c);
+ points.add(p);
+ }
+
+ long t0 = System.nanoTime();
+ Iterator<Point> pointIt = points.iterator();
+ Iterator<Polygon> pit = polygons.iterator();
+ for (int k = 0; pit.hasNext() && pointIt.hasNext(); k++) {
+ Polygon poly = pit.next();
+ Point p = pointIt.next();
+ if (p.within(poly)) {
+ count++;
+ }
+ }
+ long t1 = System.nanoTime();
+ System.out
+ .println("within alternative port2 : calc points first, outside timer, skip bbox");
+ Compare.report_within(t1 - t0, polygons.size(), count, -1);
+ }
+
+
+
+ // new not in c++ original
+ if (Compare.MEASURE_CONTAINS) {
+ int count = 0;
+ List<Point> points = new ArrayList<Point>(boxes.size());
+ for (int e = 0; e < boxes.size(); e++) {
+ Envelope b = boxes.get(e);
+ Coordinate c = b.centre();
+ Point p = geometryFactory.createPoint(c);
+ points.add(p);
+ }
+ long t0 = System.nanoTime();
+ Iterator<Point> pointIt = points.iterator();
+ Iterator<Polygon> pit = polygons.iterator();
+ for (int k = 0; pit.hasNext() && pointIt.hasNext(); k++) {
+ Polygon poly = pit.next();
+ Point p = pointIt.next();
+ if (poly.contains(p)) {
+ count++;
+ }
+ }
+ long t1 = System.nanoTime();
+ Compare.report_contains(t1 - t0, polygons.size(), count, -1);
+ }
+
+ // new not in c++ original, prepared geometry
+ if (Compare.MEASURE_CONTAINS) {
+ int count = 0;
+ List<Point> points = new ArrayList<Point>(boxes.size());
+ for (int e = 0; e < boxes.size(); e++) {
+ Envelope b = boxes.get(e);
+ Coordinate c = b.centre();
+ Point p = geometryFactory.createPoint(c);
+ points.add(p);
+ }
+
+ // and run it again: should be fatser now ???
+ long t0 = System.nanoTime();
+ Iterator<Point> pointIt = points.iterator();
+ Iterator<PreparedPolygon> pit = ppolygons.iterator();
+ for (int k = 0; pit.hasNext() && pointIt.hasNext(); k++) {
+ PreparedPolygon poly = pit.next();
+ Point p = pointIt.next();
+ if (poly.contains(p)) {
+ count++;
+ }
+ }
+ long t1 = System.nanoTime();
+ System.out.println("rerun of contains using prepared polygons");
+ Compare.report_contains(t1 - t0, polygons.size(), count, -1);
+
+ }
+
+ // // from geos c++ original , commented: too slow: full n*n comparison
+ // if (Compare.MEASURE_TOUCH) {
+ // int count = 0;
+ // int count_non_disjoint = 0;
+ // long t0 = System.nanoTime();
+ //
+ // int e1 = 0;
+ //
+ // for (Iterator<Polygon> it1 = polygons.iterator(); it1.hasNext();
+ // e1++) {
+ // Polygon polygon1 = it1.next();
+ // Envelope env1 = polygon1.getEnvelopeInternal();
+ // env1.expandBy(2.0);
+ // int e2 = 0;
+ // for (Iterator<Polygon> it2 = polygons.iterator(); it2.hasNext();
+ // e2++) {
+ // Polygon polygon2 = it2.next();
+ // Envelope env2 = polygon2.getEnvelopeInternal();
+ // env2.expandBy(2.0);
+ // if (env1.intersects(env2)) {
+ // count_non_disjoint++;
+ // if (!(polygon1.disjoint(polygon2))) {
+ // count++;
+ // }
+ // }
+ // }
+ // }
+ // long t1 = System.nanoTime();
+ // Compare.report_touch(t1 - t0, polygons.size(), count,
+ // count_non_disjoint);
+ // }
+
+ }
+
+ public static void main(String[] args) throws Exception {
+ GeomBenchmark c = new GeomBenchmark();
+ File file = new File("c:/data/spatial/shape/c100000jan2902.shp");
+ c.prepare(file);
+ // wait
+ c.runchecks();
+ }
+
+ public static void read_shapefile(File file, List<Polygon> polygons,
+ List<Integer> ids) {
+ try {
+ /*
+ * Attmpt to find a GeoTools DataStore that can handle the shapefile
+ */
+ Map<String, Serializable> connectParameters = new HashMap<String, Serializable>();
+
+ connectParameters.put("url", file.toURI().toURL());
+ connectParameters.put("create spatial index", false);
+
+ DataStore dataStore = DataStoreFinder
+ .getDataStore(connectParameters);
+ if (dataStore == null) {
+ System.out.println("No DataStore found to handle"
+ + file.getPath());
+ System.exit(1);
+ }
+
+ /*
+ * We are now connected to the shapefile. Get the type name of the
+ * features within it
+ */
+ String[] typeNames = dataStore.getTypeNames();
+ String typeName = typeNames[0];
+
+ System.out.println("Reading content " + typeName);
+
+ /*
+ * Iterate through the features, collecting some spatial data (line
+ * or boundary length) on each one
+ */
+ FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
+ FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
+ FeatureIterator<SimpleFeature> iterator;
+
+ featureSource = dataStore.getFeatureSource(typeName);
+ collection = featureSource.getFeatures();
+ iterator = collection.features();
+
+ int i = 0;
+ double totalArea = 0.0;
+ try {
+ while (iterator.hasNext()) {
+ SimpleFeature feature = iterator.next();
+
+ i++;
+ // System.out.println(i+" :"+feature.getID());
+ /*
+ * The spatial portion of the feature is represented by a
+ * Geometry object
+ */
+ Geometry geometry = (Geometry) feature.getDefaultGeometry();
+ // TODO validate polygon?
+
+ // Process only polygons, and from them only single-polygons
+ // without holes
+ Polygon polygon = null;
+ if (geometry instanceof Polygon) {
+ polygon = (Polygon) geometry;
+ } else if (geometry instanceof MultiPolygon) {
+ MultiPolygon mp = (MultiPolygon) geometry;
+ if (mp.getNumGeometries() == 1) {
+ polygon = (Polygon) mp.getGeometryN(0);
+ } else {
+ /*System.out
+ .println(i
+ + "skipped: not a single polygon multipolygon");
+ */
+ }
+ } else {
+ System.out.println(i
+ + " skipped: not a (multi)polygon:"
+ + geometry.getGeometryType());
+ }
+
+ if (polygon != null) {
+ if (polygon.getNumInteriorRing() == 0) {
+ totalArea += polygon.getArea();
+ polygons.add(polygon);
+ ids.add(i);
+ } else {
+ /*System.out.println(i
+ + " not a single ring polygon:"
+ + geometry.getGeometryType());
+ */
+ }
+ }
+
+ }
+ } finally {
+ /*
+ * You MUST explicitly close the feature iterator otherwise
+ * terrible things will happen !!!
+ */
+ if (iterator != null) {
+ iterator.close();
+ }
+ }
+
+ System.out.println("Total Area" + totalArea);
+
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ System.exit(1);
+ }
+ }
+}
\ No newline at end of file


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk