Strabon

changeset 83:6f653aacd449

Introduce JTSWrapper which is a singleton and needs to be used for WK?reader.read/WK?writer.write calls which have to be synchronized. Fixes bug for storing geometries.
author Babis Nikolaou <charnik@di.uoa.gr>
date Fri Mar 30 16:00:13 2012 +0300 (2012-03-30)
parents e2f2da9fceb5
children 5c698cb0a3d5
files evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPolyhedron.java evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/JTSWrapper.java
line diff
     1.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPolyhedron.java	Fri Mar 30 13:52:57 2012 +0300
     1.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPolyhedron.java	Fri Mar 30 16:00:13 2012 +0300
     1.3 @@ -1,6 +1,17 @@
     1.4  package org.openrdf.query.algebra.evaluation.function.spatial;
     1.5  import java.util.ArrayList;
     1.6  
     1.7 +import org.geotools.geometry.jts.JTS;
     1.8 +import org.geotools.referencing.CRS;
     1.9 +import org.opengis.geometry.MismatchedDimensionException;
    1.10 +import org.opengis.referencing.FactoryException;
    1.11 +import org.opengis.referencing.crs.CoordinateReferenceSystem;
    1.12 +import org.opengis.referencing.operation.MathTransform;
    1.13 +import org.opengis.referencing.operation.TransformException;
    1.14 +import org.openrdf.model.URI;
    1.15 +import org.openrdf.model.Value;
    1.16 +import org.openrdf.query.algebra.evaluation.util.JTSWrapper;
    1.17 +
    1.18  import com.vividsolutions.jts.geom.Coordinate;
    1.19  import com.vividsolutions.jts.geom.CoordinateSequence;
    1.20  import com.vividsolutions.jts.geom.Geometry;
    1.21 @@ -15,24 +26,12 @@
    1.22  import com.vividsolutions.jts.geom.Polygon;
    1.23  import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
    1.24  import com.vividsolutions.jts.io.ParseException;
    1.25 -import com.vividsolutions.jts.io.WKBReader;
    1.26 -import com.vividsolutions.jts.io.WKBWriter;
    1.27 -import com.vividsolutions.jts.io.WKTReader;
    1.28 -import com.vividsolutions.jts.io.WKTWriter;
    1.29 -
    1.30 -import org.geotools.geometry.jts.JTS;
    1.31 -import org.geotools.referencing.CRS;
    1.32 -import org.opengis.geometry.MismatchedDimensionException;
    1.33 -import org.opengis.referencing.FactoryException;
    1.34 -import org.opengis.referencing.NoSuchAuthorityCodeException;
    1.35 -import org.opengis.referencing.crs.CoordinateReferenceSystem;
    1.36 -import org.opengis.referencing.operation.MathTransform;
    1.37 -import org.opengis.referencing.operation.TransformException;
    1.38 -import org.openrdf.model.URI;
    1.39 -import org.openrdf.model.Value; 
    1.40  
    1.41  public class StrabonPolyhedron implements Value {
    1.42  
    1.43 +
    1.44 +	private static final long serialVersionUID = 894529468109904724L;
    1.45 +	
    1.46  	public static String CACHEPATH = "";
    1.47  	public static String TABLE_COUNTS = "counts.bin";
    1.48  	public static String TABLE_SUBJ_OBJ_TYPES = "tableProperties.bin";
    1.49 @@ -132,6 +131,11 @@
    1.50  
    1.51  	private static int MAX_POINTS = Integer.MAX_VALUE;//40000;//Integer.MAX_VALUE;//10000;
    1.52  
    1.53 +	/**
    1.54 +	 * Get JTS singleton instance.
    1.55 +	 */
    1.56 +	private static JTSWrapper jts = JTSWrapper.getInstance();
    1.57 +	
    1.58  	private Geometry geometry;
    1.59  
    1.60  	public StrabonPolyhedron() {
    1.61 @@ -147,7 +151,7 @@
    1.62  
    1.63  
    1.64  	public static StrabonPolyhedron ConstructFromWKB(byte[] byteArray) throws Exception {
    1.65 -		return new StrabonPolyhedron(new WKBReader().read(byteArray));
    1.66 +		return new StrabonPolyhedron(jts.WKBread((byteArray)));
    1.67  	}
    1.68  
    1.69  	public static Geometry convertSRID(Geometry A, int sourceSRID, int targetSRID)
    1.70 @@ -184,7 +188,7 @@
    1.71  
    1.72  	//public StrabonPolyhedron(int partitionAlgorithmIgnored, String constraints) throws Exception {
    1.73  	//	Polyhedron poly = new Polyhedron(constraints);
    1.74 -	//	this.geometry = new WKTReader().read(poly.toWKT());
    1.75 +	//	this.geometry = jts.WKTread(poly.toWKT());
    1.76  	//}
    1.77  
    1.78  
    1.79 @@ -634,15 +638,14 @@
    1.80  				geometry.startsWith("MULTILINESTRING") || 
    1.81  				geometry.startsWith("MULTIPOLYGON") || 
    1.82  				geometry.startsWith("GEOMETRYCOLLECTION")) {
    1.83 -			Geometry geo = new WKTReader().read(geometry);
    1.84 +			Geometry geo = jts.WKTread(geometry);
    1.85  			this.geometry = new StrabonPolyhedron(geo).geometry;
    1.86  			//Default 
    1.87  			this.geometry.setSRID(geomSRID);
    1.88  		} else {
    1.89  			//Polyhedron polyhedron = new Polyhedron(geometry);
    1.90  			//String polyhedronWKT = polyhedron.toWKT();
    1.91 -			//WKTReader reader = new WKTReader();
    1.92 -			//Geometry geo = reader.read(polyhedronWKT);
    1.93 +			//Geometry geo = jts.WKTread(polyhedronWKT);
    1.94  			//
    1.95  			//if (!EnableConstraintRepresentation) {
    1.96  			//	this.geometry = geo.union(geo);
    1.97 @@ -651,24 +654,26 @@
    1.98  	}
    1.99  
   1.100  	public StrabonPolyhedron(String WKT, int algorithm) throws Exception {
   1.101 -		Geometry geo = new WKTReader().read(WKT);
   1.102 +		System.out.println("	new StrabonPolyhedron: before WKTReader");
   1.103 +		Geometry geo = jts.WKTread(WKT);
   1.104 +		System.out.println("	new StrabonPolyhedron: after WKTReader");
   1.105  		this.geometry = new StrabonPolyhedron(geo, algorithm).geometry;
   1.106  	}
   1.107  
   1.108  	public StrabonPolyhedron(String WKT, int algorithm, int maxPoints) throws Exception {
   1.109 -		Geometry geo = new WKTReader().read(WKT);
   1.110 +		Geometry geo = jts.WKTread(WKT);
   1.111  		this.geometry = new StrabonPolyhedron(geo, algorithm).geometry;
   1.112  	}
   1.113  
   1.114  	public StrabonPolyhedron(byte[] byteArray) throws ParseException {
   1.115  
   1.116 -		this.geometry = new WKBReader().read(byteArray);
   1.117 +		this.geometry = jts.WKBread(byteArray);
   1.118  		//		System.out.println(geometry.toString()+" "+geometry.getSRID());
   1.119  	}
   1.120  
   1.121  	public StrabonPolyhedron(byte[] byteArray, int srid) throws ParseException {
   1.122  
   1.123 -		this.geometry = new WKBReader().read(byteArray);
   1.124 +		this.geometry = jts.WKBread(byteArray);
   1.125  		this.geometry.setSRID(srid);
   1.126  	}
   1.127  
   1.128 @@ -695,17 +700,15 @@
   1.129  	}
   1.130  
   1.131  	public byte[] toWKB() {
   1.132 -		WKBWriter writer = new WKBWriter();
   1.133 -		return writer.write(this.geometry);		
   1.134 +		return jts.WKBwrite(this.geometry);		
   1.135  	}
   1.136  
   1.137  	public String toWKT() {
   1.138 -		WKTWriter writer = new WKTWriter();
   1.139 -		return writer.write(this.geometry);		
   1.140 +		return jts.WKTwrite(this.geometry);		
   1.141  	}
   1.142  
   1.143  	public byte[] toByteArray() {
   1.144 -		return new WKBWriter().write(this.geometry);
   1.145 +		return jts.WKBwrite(this.geometry);
   1.146  	}
   1.147  
   1.148  
   1.149 @@ -939,7 +942,7 @@
   1.150  						+ "342164.38954080583 5536425.686612717" + "))";
   1.151  
   1.152  				start = System.nanoTime();
   1.153 -				Geometry geo = new WKTReader().read(WKT);
   1.154 +				Geometry geo = jts.WKTread(WKT);
   1.155  				geo.isValid();
   1.156  				geo.isSimple();
   1.157  				geo.normalize();
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/JTSWrapper.java	Fri Mar 30 16:00:13 2012 +0300
     2.3 @@ -0,0 +1,61 @@
     2.4 +/**
     2.5 + * 
     2.6 + */
     2.7 +package org.openrdf.query.algebra.evaluation.util;
     2.8 +
     2.9 +import com.vividsolutions.jts.geom.Geometry;
    2.10 +import com.vividsolutions.jts.io.ParseException;
    2.11 +import com.vividsolutions.jts.io.WKBReader;
    2.12 +import com.vividsolutions.jts.io.WKBWriter;
    2.13 +import com.vividsolutions.jts.io.WKTReader;
    2.14 +import com.vividsolutions.jts.io.WKTWriter;
    2.15 +
    2.16 +/**
    2.17 + * This class is a singleton and provides access to the readers/writers
    2.18 + * of Java Topology Suite. 
    2.19 + * 
    2.20 + * @author charnik
    2.21 + *
    2.22 + */
    2.23 +public class JTSWrapper {
    2.24 +
    2.25 +	// single instance of JTSReaders
    2.26 +	private static JTSWrapper instance;
    2.27 +	
    2.28 +	private WKTReader wktr;
    2.29 +	private WKTWriter wktw;
    2.30 +	private WKBReader wkbr;
    2.31 +	private WKBWriter wkbw;
    2.32 +	
    2.33 +	private JTSWrapper() {
    2.34 +		// use a private constructor to force call of getInstance method and forbid subclassing
    2.35 +		wktr = new WKTReader();
    2.36 +		wktw = new WKTWriter();
    2.37 +		wkbr = new WKBReader();
    2.38 +		wkbw = new WKBWriter();
    2.39 +	}
    2.40 +	
    2.41 +	public static synchronized JTSWrapper getInstance() {
    2.42 +		if (instance == null) {
    2.43 +			instance = new JTSWrapper();
    2.44 +		}
    2.45 +		return instance;
    2.46 +	}
    2.47 +	
    2.48 +	public synchronized Geometry WKTread(String wkt) throws ParseException {
    2.49 +		return wktr.read(wkt);
    2.50 +	}
    2.51 +	
    2.52 +	public synchronized String WKTwrite(Geometry geom) {
    2.53 +		return wktw.write(geom);
    2.54 +	}
    2.55 +	
    2.56 +	public synchronized Geometry WKBread(byte[] bytes) throws ParseException {
    2.57 +		return wkbr.read(bytes);
    2.58 +	}
    2.59 +	
    2.60 +	public synchronized byte[] WKBwrite(Geometry geom) {
    2.61 +		return wkbw.write(geom);
    2.62 +	}
    2.63 +	
    2.64 +}