Strabon

changeset 386:c45fb8bccd90

refactorings in StrabonPolyhedron. Moved GMLReader to JTSWrapper
author Babis Nikolaou <charnik@di.uoa.gr>
date Wed Jun 27 19:30:50 2012 +0300 (2012-06-27)
parents 75503b0be891
children 74c6142de217
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 generaldb/src/main/java/org/openrdf/sail/generaldb/schema/LiteralTable.java
line diff
     1.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPolyhedron.java	Wed Jun 27 18:36:49 2012 +0300
     1.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/function/spatial/StrabonPolyhedron.java	Wed Jun 27 19:30:50 2012 +0300
     1.3 @@ -78,113 +78,112 @@
     1.4  	 */
     1.5  	private Geometry geometry;
     1.6  
     1.7 +	/**
     1.8 +	 * Creates a {@link StrabonPolyhedron} instance with an empty geometry.
     1.9 +	 */
    1.10  	public StrabonPolyhedron() {
    1.11  		this.geometry = null;
    1.12  
    1.13  	}
    1.14  	
    1.15 -	public StrabonPolyhedron(Polygon polygon) throws Exception {
    1.16 -		this.geometry = new StrabonPolyhedron(polygon, 1).geometry;
    1.17 +	/**
    1.18 +	 * Creates a {@link StrabonPolyhedron} instance with the given geometry.
    1.19 +	 * 
    1.20 +	 * @param geo
    1.21 +	 * @throws Exception
    1.22 +	 */
    1.23 +	public StrabonPolyhedron(Geometry geo) throws Exception {
    1.24 +		this.geometry = new StrabonPolyhedron(geo, 1).geometry;
    1.25 +		this.geometry.setSRID(geo.getSRID());
    1.26  	}
    1.27 -
    1.28 -	public StrabonPolyhedron(String geometry) throws Exception {
    1.29 -		int geomSRID = 4326;
    1.30 -		if(geometry.contains(";"))
    1.31 -		{
    1.32 -			int whereToCut = geometry.lastIndexOf('/');
    1.33 -			geomSRID = Integer.parseInt(geometry.substring(whereToCut+1));
    1.34 -			whereToCut = geometry.indexOf(';');
    1.35 -			geometry.substring(0,whereToCut);
    1.36 -		}
    1.37 -		if (geometry.startsWith("POINT") || 
    1.38 -				geometry.startsWith("LINESTRING") || 
    1.39 -				geometry.startsWith("POLYGON") || 
    1.40 -				geometry.startsWith("MULTIPOINT") || 
    1.41 -				geometry.startsWith("MULTILINESTRING") || 
    1.42 -				geometry.startsWith("MULTIPOLYGON") || 
    1.43 -				geometry.startsWith("GEOMETRYCOLLECTION")) {
    1.44 -			Geometry geo = jts.WKTread(geometry);
    1.45 -			this.geometry = new StrabonPolyhedron(geo).geometry;
    1.46 -			//Default 
    1.47 -			this.geometry.setSRID(geomSRID);
    1.48 -		} else {
    1.49 -		
    1.50 -			if(geometry.contains("gml"))
    1.51 -			{
    1.52 -				Geometry geo = GMLReader(geometry);
    1.53 -				this.geometry = new StrabonPolyhedron(geo).geometry;
    1.54 +	
    1.55 +	/**
    1.56 +	 * Creates a {@link StrabonPolyhedron} instance with a geometry given
    1.57 +	 * in the representation of the argument. The representation could be
    1.58 +	 * either in WKT or in GML.
    1.59 +	 * 
    1.60 +	 * @param representation
    1.61 +	 * @throws Exception
    1.62 +	 */
    1.63 +	public StrabonPolyhedron(String representation) {
    1.64 +		try {
    1.65 +			// try first as WKT
    1.66 +			geometry = jts.WKTread(representation);
    1.67 +			
    1.68 +		} catch (ParseException e) {
    1.69 +			try {
    1.70 +				// try as GML
    1.71 +				geometry = JTSWrapper.GMLReader(representation);
    1.72 +				
    1.73 +			} catch (Exception e1) {
    1.74 +				throw new IllegalArgumentException(e1);
    1.75  			}
    1.76 -			
    1.77 -			//Polyhedron polyhedron = new Polyhedron(geometry);
    1.78 -			//String polyhedronWKT = polyhedron.toWKT();
    1.79 -			//Geometry geo = jts.WKTread(polyhedronWKT);
    1.80 -			//
    1.81 -			//if (!EnableConstraintRepresentation) {
    1.82 -			//	this.geometry = geo.union(geo);
    1.83 -			//}
    1.84  		}
    1.85  	}
    1.86  
    1.87 +	/**
    1.88 +	 * Creates a {@link StrabonPolyhedron} instance with a geometry represented 
    1.89 +	 * by the given byte array.
    1.90 +	 * 
    1.91 +	 * @param byteArray
    1.92 +	 * @throws ParseException
    1.93 +	 */
    1.94 +	public StrabonPolyhedron(byte[] byteArray) throws ParseException {
    1.95 +		this.geometry = jts.WKBread(byteArray);
    1.96 +	}
    1.97 +
    1.98 +	/**
    1.99 +	 * Creates a {@link StrabonPolyhedron} instance with a geometry represented
   1.100 +	 * by the given byte array and sets the SRID of the geometry to the given one.
   1.101 +	 * 
   1.102 +	 * @param byteArray
   1.103 +	 * @param srid
   1.104 +	 * @throws ParseException
   1.105 +	 */
   1.106 +	public StrabonPolyhedron(byte[] byteArray, int srid) throws ParseException {
   1.107 +		this(byteArray);
   1.108 +		this.geometry.setSRID(srid);
   1.109 +	}
   1.110 +	
   1.111 +	/**
   1.112 +	 * Returns the string representation of the geometry of this 
   1.113 +	 * {@link StrabonPolyhedron} instance. The result of this method
   1.114 +	 * is the same to the one of method {@link #toWKT()}.
   1.115 +	 */
   1.116 +	public String stringValue() {
   1.117 +		return toWKT();
   1.118 +	}
   1.119 +
   1.120 +	@Override
   1.121 +	public boolean equals(Object other) {
   1.122 +		if (this == other) {
   1.123 +			return true;
   1.124 +		}
   1.125 +		
   1.126 +		if(other instanceof StrabonPolyhedron) {
   1.127 +			if (((StrabonPolyhedron) other).geometry.equals(this.getGeometry())) {
   1.128 +				return true;
   1.129 +			}
   1.130 +		}
   1.131 +		return false;
   1.132 +	}
   1.133 +	
   1.134 +	@Deprecated
   1.135  	public StrabonPolyhedron(String WKT, int algorithm) throws Exception {
   1.136  		if(WKT.contains("gml"))
   1.137  		{
   1.138 -			Geometry geo = GMLReader(WKT);
   1.139 +			Geometry geo = JTSWrapper.GMLReader(WKT);
   1.140  			this.geometry = new StrabonPolyhedron(geo).geometry;
   1.141  		}
   1.142  		else
   1.143  		{
   1.144 -			//System.out.println("	new StrabonPolyhedron: before WKTReader");
   1.145  			Geometry geo = jts.WKTread(WKT);
   1.146 -			//System.out.println("	new StrabonPolyhedron: after WKTReader");
   1.147  			this.geometry = new StrabonPolyhedron(geo, algorithm).geometry;
   1.148  		}
   1.149  	
   1.150  	}
   1.151  
   1.152 -	public StrabonPolyhedron(String WKT, int algorithm, int maxPoints) throws Exception {
   1.153 -
   1.154 -		if(WKT.contains("gml"))
   1.155 -		{
   1.156 -			System.err.println("**************** THIS IS NOT A GOOD PLACE/WARY FOR THIS CONVERSION /////////");
   1.157 -			Geometry geo = GMLReader(WKT);
   1.158 -			this.geometry = new StrabonPolyhedron(geo).geometry;
   1.159 -		}
   1.160 -		else
   1.161 -		{
   1.162 -
   1.163 -			Geometry geo = jts.WKTread(WKT);
   1.164 -			this.geometry = new StrabonPolyhedron(geo, algorithm).geometry;	
   1.165 -		}
   1.166 -	}
   1.167 -
   1.168 -	public StrabonPolyhedron(byte[] byteArray) throws ParseException {
   1.169 -		this.geometry = jts.WKBread(byteArray);
   1.170 -	}
   1.171 -
   1.172 -	public StrabonPolyhedron(byte[] byteArray, int srid) throws ParseException {
   1.173 -
   1.174 -		this.geometry = jts.WKBread(byteArray);
   1.175 -		this.geometry.setSRID(srid);
   1.176 -	}
   1.177 -
   1.178 -	public void setGeometry(Geometry geometry) {
   1.179 -		this.geometry = geometry;
   1.180 -	}
   1.181 -
   1.182 -	public static StrabonPolyhedron ConstructFromWKB(byte[] byteArray) throws Exception {
   1.183 -		return new StrabonPolyhedron(jts.WKBread((byteArray)));
   1.184 -	}
   1.185 -
   1.186 -	//public StrabonPolyhedron(int partitionAlgorithmIgnored, String constraints) throws Exception {
   1.187 -	//	Polyhedron poly = new Polyhedron(constraints);
   1.188 -	//	this.geometry = jts.WKTread(poly.toWKT());
   1.189 -	//}
   1.190 -
   1.191 -	public StrabonPolyhedron(Geometry geo) throws Exception {
   1.192 -		this.geometry = new StrabonPolyhedron(geo, 1).geometry;
   1.193 -		this.geometry.setSRID(geo.getSRID());
   1.194 -	}
   1.195 -
   1.196 +	@Deprecated
   1.197  	public StrabonPolyhedron(Geometry geo, int algorithm) throws Exception {
   1.198  		this.geometry = new StrabonPolyhedron(geo, algorithm, MAX_POINTS).geometry;
   1.199  	}
   1.200 @@ -335,6 +334,70 @@
   1.201  		}
   1.202  	}
   1.203  
   1.204 +	/**
   1.205 +	 * Sets the geometry of this {@link StrabonPolyhedron} instance to
   1.206 +	 * the given one.
   1.207 +	 * 
   1.208 +	 * @param geometry
   1.209 +	 */
   1.210 +	public void setGeometry(Geometry geometry) {
   1.211 +		this.geometry = geometry;
   1.212 +	}
   1.213 +
   1.214 +	/**
   1.215 +	 * Returns the string representation of the geometry.
   1.216 +	 */
   1.217 +	public String toString() {
   1.218 +		return geometry.toString();
   1.219 +	}
   1.220 +
   1.221 +	/**
   1.222 +	 * Returns the representation of the geometry in WKT (assumed 
   1.223 +	 * as the default representation in {@link StrabonPolyhedron}).
   1.224 +	 * 
   1.225 +	 * @return
   1.226 +	 */
   1.227 +	public String toText() {
   1.228 +		return geometry.toText();
   1.229 +	}
   1.230 +
   1.231 +	/**
   1.232 +	 * Return the geometry of {@link StrabonPolyhedron} in Well-Known
   1.233 +	 * Binary (WKB).
   1.234 +	 * 
   1.235 +	 * @return
   1.236 +	 */
   1.237 +	public byte[] toWKB() {
   1.238 +		return jts.WKBwrite(this.geometry);		
   1.239 +	}
   1.240 +
   1.241 +	/**
   1.242 +	 * Return the geometry of {@link StrabonPolyhedron} as WKT.
   1.243 +	 * 
   1.244 +	 * @return
   1.245 +	 */
   1.246 +	public String toWKT() {
   1.247 +		return jts.WKTwrite(this.geometry);		
   1.248 +	}
   1.249 +
   1.250 +	/**
   1.251 +	 * Return the geometry of {@link StrabonPolyhedron} as a byte array.
   1.252 +	 * 
   1.253 +	 * @return
   1.254 +	 */
   1.255 +	public byte[] toByteArray() {
   1.256 +		return jts.WKBwrite(this.geometry);
   1.257 +	}
   1.258 +
   1.259 +	/**
   1.260 +	 * Returns the geometry of this {@link StrabonPolyhedron} instance.
   1.261 +	 * 
   1.262 +	 * @return
   1.263 +	 */
   1.264 +	public Geometry getGeometry() {
   1.265 +		return this.geometry;
   1.266 +	}
   1.267 +	
   1.268  	public static StrabonPolyhedron ParseBigPolyhedron(Geometry polygon, int algorithm, boolean horizontal, int maxPoints) throws Exception {
   1.269  		assert (Polygon.class.isInstance(polygon) || (MultiPolygon.class.isInstance(polygon)));
   1.270  
   1.271 @@ -409,6 +472,7 @@
   1.272  		}
   1.273  	}
   1.274  
   1.275 +	@Deprecated
   1.276  	public StrabonPolyhedron(Polygon polygon, int algorithm, int maxPoints) throws Exception {
   1.277  		//		if (!polygon.isSimple())
   1.278  		//			throw new Exception(
   1.279 @@ -476,32 +540,6 @@
   1.280  			c[distinctCoordinates-1][1] = coordinates[coordinates.length-1].y;
   1.281  		}
   1.282  
   1.283 -		//System.out.println("--- Counter              = " + counter);
   1.284 -		//System.out.println("---\n---\n---\n---\n---\n");
   1.285 -
   1.286 -		//		BufferedWriter bww = new BufferedWriter(new FileWriter(new File("/home/kkyzir/Desktop/Spatial data/ssg4env/geometries/gnuplot/cfunction.dat")));
   1.287 -		//		BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File("/home/kkyzir/Desktop/Spatial data/ssg4env/geometries/gnuplot/original.dat")));
   1.288 -		//		bww.write("void make_polygon(Polygon_2& polygon) {");
   1.289 -		//		for (int i = 0; i < coordinates.length - 1; i++) {
   1.290 -		//			Coordinate coordinate = coordinates[i];
   1.291 -		//			bww.write("\tpolygon.push_back(Point_2(");
   1.292 -		//			bww.write(new Double(coordinate.x).toString());
   1.293 -		//			bww.write(",");
   1.294 -		//			bww.write(new Double(coordinate.y).toString());
   1.295 -		//			bww.write("));\n");
   1.296 -		//			
   1.297 -		//			bw2.write(new Double(coordinate.x).toString());
   1.298 -		//			bw2.write(" ");
   1.299 -		//			bw2.write(new Double(coordinate.y).toString());
   1.300 -		//			bw2.write("\n");
   1.301 -		//		}
   1.302 -		//		bww.write("}\n");
   1.303 -		//		bww.flush();
   1.304 -		//		bww.close();
   1.305 -		//		
   1.306 -		//		bw2.flush();
   1.307 -		//		bw2.close();
   1.308 -
   1.309  		double start = System.nanoTime();
   1.310  		//		double[][][] convexified = Polyhedron.ConvexifyPolygon(c, algorithm);
   1.311  		double[][][] convexified = new double[1][2][3];		
   1.312 @@ -619,26 +657,6 @@
   1.313  		return "";
   1.314  	}
   1.315  
   1.316 -	public String toString() {
   1.317 -		return this.geometry.toString();
   1.318 -	}
   1.319 -
   1.320 -	public String toText() {
   1.321 -		return this.geometry.toText();
   1.322 -	}
   1.323 -
   1.324 -	public byte[] toWKB() {
   1.325 -		return jts.WKBwrite(this.geometry);		
   1.326 -	}
   1.327 -
   1.328 -	public String toWKT() {
   1.329 -		return jts.WKTwrite(this.geometry);		
   1.330 -	}
   1.331 -
   1.332 -	public byte[] toByteArray() {
   1.333 -		return jts.WKBwrite(this.geometry);
   1.334 -	}
   1.335 -
   1.336  	public static StrabonPolyhedron union(StrabonPolyhedron A, StrabonPolyhedron B) throws Exception {
   1.337  		StrabonPolyhedron poly = new StrabonPolyhedron();
   1.338  
   1.339 @@ -796,7 +814,6 @@
   1.340  		return poly;
   1.341  	}
   1.342  
   1.343 -
   1.344  	public StrabonPolyhedron getBuffer(double distance) throws Exception {
   1.345  		Geometry geo = this.geometry.buffer(distance);
   1.346  		return new StrabonPolyhedron(geo);
   1.347 @@ -815,11 +832,7 @@
   1.348  	public double getArea() throws Exception {
   1.349  		return this.getArea();
   1.350  	}
   1.351 -
   1.352 -	public Geometry getGeometry() {
   1.353 -		return this.geometry;
   1.354 -	}
   1.355 -
   1.356 +	
   1.357  	public int getNumPoints() {
   1.358  		return this.geometry.getNumPoints();
   1.359  	}
   1.360 @@ -835,35 +848,4 @@
   1.361  										GeometryCollection.class.isInstance(geo) ? "GeometryCollection" : 
   1.362  											"Unknown";
   1.363  	}
   1.364 -
   1.365 -	public String stringValue() {
   1.366 -		return this.toWKT();
   1.367 -	}
   1.368 -
   1.369 -	@Override
   1.370 -	public boolean equals(Object other) {
   1.371 -
   1.372 -		if(other instanceof StrabonPolyhedron)
   1.373 -		{
   1.374 -			if (((StrabonPolyhedron) other).geometry.equals(this.getGeometry()))
   1.375 -			{
   1.376 -				return true;
   1.377 -			}
   1.378 -
   1.379 -		}
   1.380 -		return false;
   1.381 -	}
   1.382 -	
   1.383 -	public Geometry GMLReader(String GML) throws IOException, SAXException, ParserConfigurationException, JAXBException
   1.384 -	{
   1.385 -        StringReader reader = new StringReader(GML);
   1.386 -		JAXBContext context=JAXBContext.newInstance("org.jvnet.ogc.gml.v_3_1_1.jts");	
   1.387 -		//Point point = (Point) context.createUnmarshaller().unmarshal(getClass().getResourceAsStream(inputstream));
   1.388 -		Unmarshaller unmarshaller = context.createUnmarshaller();
   1.389 -		Geometry geometry = (Geometry) unmarshaller.unmarshal(reader);
   1.390 -		if(geometry.getSRID()>0)
   1.391 -			 System.out.println("GML Geometry SRID: "+geometry.getSRID());
   1.392 -		reader.close();
   1.393 -        return  geometry;
   1.394 -	}
   1.395  }
     2.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/JTSWrapper.java	Wed Jun 27 18:36:49 2012 +0300
     2.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/JTSWrapper.java	Wed Jun 27 19:30:50 2012 +0300
     2.3 @@ -3,6 +3,14 @@
     2.4   */
     2.5  package org.openrdf.query.algebra.evaluation.util;
     2.6  
     2.7 +import java.io.IOException;
     2.8 +import java.io.StringReader;
     2.9 +
    2.10 +import javax.xml.bind.JAXBContext;
    2.11 +import javax.xml.bind.JAXBException;
    2.12 +import javax.xml.bind.Unmarshaller;
    2.13 +import javax.xml.parsers.ParserConfigurationException;
    2.14 +
    2.15  import org.geotools.geometry.jts.JTS;
    2.16  import org.geotools.referencing.CRS;
    2.17  import org.opengis.geometry.MismatchedDimensionException;
    2.18 @@ -12,6 +20,7 @@
    2.19  import org.opengis.referencing.operation.TransformException;
    2.20  import org.slf4j.Logger;
    2.21  import org.slf4j.LoggerFactory;
    2.22 +import org.xml.sax.SAXException;
    2.23  
    2.24  import com.vividsolutions.jts.geom.Geometry;
    2.25  import com.vividsolutions.jts.io.ParseException;
    2.26 @@ -56,6 +65,7 @@
    2.27  	 */
    2.28  	private WKBWriter wkbw;
    2.29  	
    2.30 +	
    2.31  	private JTSWrapper() {
    2.32  		// use a private constructor to force call of getInstance method and forbid subclassing
    2.33  		wktr = new WKTReader();
    2.34 @@ -138,4 +148,26 @@
    2.35  		return output;
    2.36  	}
    2.37  	
    2.38 +	/**
    2.39 +	 * Parses and returns a {@link Geometry} object constructed from the given GML representation.
    2.40 +	 * 
    2.41 +	 * @param gml
    2.42 +	 * @return
    2.43 +	 * @throws IOException
    2.44 +	 * @throws SAXException
    2.45 +	 * @throws ParserConfigurationException
    2.46 +	 * @throws JAXBException
    2.47 +	 */
    2.48 +	public static Geometry GMLReader(String gml) throws IOException, SAXException, ParserConfigurationException, JAXBException {
    2.49 +        StringReader reader = new StringReader(gml);
    2.50 +		
    2.51 +        JAXBContext context = JAXBContext.newInstance("org.jvnet.ogc.gml.v_3_1_1.jts");	
    2.52 +		
    2.53 +        Unmarshaller unmarshaller = context.createUnmarshaller();
    2.54 +		
    2.55 +        Geometry geometry = (Geometry) unmarshaller.unmarshal(reader);
    2.56 +		
    2.57 +		reader.close();
    2.58 +        return geometry;
    2.59 +	}
    2.60  }
     3.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/schema/LiteralTable.java	Wed Jun 27 18:36:49 2012 +0300
     3.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/schema/LiteralTable.java	Wed Jun 27 19:30:50 2012 +0300
     3.3 @@ -173,7 +173,7 @@
     3.4  			geomWKB = polyhedron.toByteArray();
     3.5  			
     3.6  		
     3.7 -		} catch (conversionException e) {
     3.8 +		} catch (IllegalArgumentException e) {
     3.9  			e.printStackTrace();
    3.10  		} catch (Exception e) {
    3.11  			throw new SQLException("An issue occurred in the underlying StrabonPolyhedron's constructor!");
    3.12 @@ -206,11 +206,7 @@
    3.13  				System.out.println("SRID="+srid);
    3.14  			}
    3.15  			geomWKB = polyhedron.toWKB();
    3.16 -			
    3.17 -		}  catch (conversionException e) {
    3.18 -			e.printStackTrace();
    3.19 -		} catch (IOException e) {
    3.20 -			e.printStackTrace();
    3.21 +	
    3.22  		} catch (Exception e) {
    3.23  			e.printStackTrace();
    3.24  		}