| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/cfg/HSProperty.java |
|---|
| New file |
| 0,0 → 1,41 |
| /** |
| * $Id$ |
| * |
| * This file is part of Hibernate Spatial, an extension to the |
| * hibernate ORM solution for geographic data. |
| * |
| * Copyright © 2007 Geovise BVBA |
| * |
| * This work was partially supported by the European Commission, |
| * under the 6th Framework Programme, contract IST-2-004688-STP. |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General Public |
| * License along with this library; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * For more information, visit: http://www.hibernatespatial.org/ |
| */ |
| package org.hibernatespatial.cfg; |
| /** |
| * This enum contains the configurable properties of the Hibernate Spatial |
| * Extension. |
| * |
| * @author Karel Maesen |
| * |
| */ |
| public enum HSProperty { |
| DEFAULT_DIALECT, PRECISION_MODEL, PRECISION_MODEL_SCALE |
| } |
| Property changes: |
| Added: svn:keywords |
| + Id |
| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/cfg/HSConfiguration.java |
|---|
| New file |
| 0,0 → 1,161 |
| /** |
| * $Id$ |
| * |
| * This file is part of Hibernate Spatial, an extension to the |
| * hibernate ORM solution for geographic data. |
| * |
| * Copyright © 2007 Geovise BVBA |
| * |
| * This work was partially supported by the European Commission, |
| * under the 6th Framework Programme, contract IST-2-004688-STP. |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General Public |
| * License along with this library; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * For more information, visit: http://www.hibernatespatial.org/ |
| */ |
| package org.hibernatespatial.cfg; |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileNotFoundException; |
| import java.io.InputStream; |
| import java.util.Properties; |
| import org.apache.log4j.Logger; |
| import org.dom4j.Document; |
| import org.dom4j.DocumentException; |
| import org.dom4j.Element; |
| import org.dom4j.io.SAXReader; |
| import org.hibernate.cfg.Configuration; |
| /** |
| * Configuration information for the Hibernate Spatial Extension. |
| * |
| * @author Karel Maesen |
| * |
| * |
| */ |
| public class HSConfiguration extends Properties { |
| /** |
| * |
| */ |
| private static final long serialVersionUID = 1L; |
| private static Logger logger = Logger.getLogger(HSConfiguration.class); |
| private String source; |
| private HSProperty[] HSProperties; |
| public HSConfiguration() { |
| HSProperties = HSProperty.values(); |
| } |
| public String getDefaultDialect() { |
| return getProperty(HSProperty.DEFAULT_DIALECT.toString()); |
| } |
| public void setDefaultDialect(String dialect) { |
| setProperty(HSProperty.DEFAULT_DIALECT, dialect); |
| } |
| public String getPrecisionModel() { |
| return getProperty(HSProperty.PRECISION_MODEL.toString()); |
| } |
| public void setPrecisionModel(String precisionModel) { |
| setProperty(HSProperty.PRECISION_MODEL, precisionModel); |
| } |
| public String getProperty(HSProperty property) { |
| return getProperty(property.toString()); |
| } |
| public void setProperty(HSProperty property, String value) { |
| setProperty(property.toString(), value); |
| } |
| public HSConfiguration configure(Configuration hibernateConfig) { |
| String dialect = hibernateConfig.getProperty("hibernate.dialect"); |
| setProperty(HSProperty.DEFAULT_DIALECT, dialect); |
| return this; |
| } |
| public HSConfiguration configure() { |
| return configure("hibernate-spatial.cfg.xml"); |
| } |
| public HSConfiguration configure(File resource) { |
| this.source = resource.getName(); |
| logger.info("configuring from file: " + resource.getName()); |
| try { |
| return doConfigure(new FileInputStream(resource)); |
| } catch (FileNotFoundException e) { |
| logger.warn("could not find file: " + resource + ".\nCause:" |
| + e.getMessage()); |
| } catch (DocumentException e) { |
| logger.warn("Failed to load configuration file :" + resource |
| + ".\nCause:" + e.getMessage()); |
| } |
| return null; |
| } |
| /** |
| * The source file or URL for this configuration. |
| * |
| * @return The source name (file or URL). |
| */ |
| public String getSource() { |
| return this.source; |
| } |
| public HSConfiguration configure(String resource) { |
| logger.info("configuring from resource: " + resource); |
| this.source = resource; |
| ClassLoader classLoader = Thread.currentThread() |
| .getContextClassLoader(); |
| InputStream stream = null; |
| try { |
| stream = classLoader.getResourceAsStream(resource); |
| return doConfigure(stream); |
| } catch (Exception e) { |
| logger.warn("Failed to load configuration file :" + resource); |
| } |
| return null; |
| } |
| private HSConfiguration doConfigure(InputStream stream) |
| throws DocumentException { |
| try { |
| SAXReader reader = new SAXReader(); |
| Document configDoc = reader.read(stream); |
| Element root = configDoc.getRootElement(); |
| for (HSProperty hsprop : HSProperties) { |
| Element propEl = root.element(hsprop.toString().toLowerCase()); |
| if (propEl != null) { |
| setProperty(hsprop, propEl.getText()); |
| } |
| } |
| } finally { |
| try { |
| stream.close(); |
| } catch (Exception e) { |
| } // Can't do anything about this. |
| } |
| return this; |
| } |
| } |
| Property changes: |
| Added: svn:keywords |
| + Id |
| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/cfg/GeometryFactoryHelper.java |
|---|
| New file |
| 0,0 → 1,82 |
| /** |
| * $Id$ |
| * |
| * This file is part of Hibernate Spatial, an extension to the |
| * hibernate ORM solution for geographic data. |
| * |
| * Copyright © 2007 Geovise BVBA |
| * |
| * This work was partially supported by the European Commission, |
| * under the 6th Framework Programme, contract IST-2-004688-STP. |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General Public |
| * License along with this library; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * For more information, visit: http://www.hibernatespatial.org/ |
| */ |
| package org.hibernatespatial.cfg; |
| import java.util.Map; |
| import org.apache.log4j.Logger; |
| import org.hibernatespatial.mgeom.MGeometryFactory; |
| import com.vividsolutions.jts.geom.PrecisionModel; |
| public class GeometryFactoryHelper { |
| private static Logger logger = Logger |
| .getLogger(GeometryFactoryHelper.class); |
| public static MGeometryFactory createGeometryFactory(Map map) { |
| if (map == null) { |
| return new MGeometryFactory(); |
| } |
| String precisionModelName = null; |
| Double scale = null; |
| if (map.containsKey(HSProperty.PRECISION_MODEL.toString())) { |
| precisionModelName = (String) map.get(HSProperty.PRECISION_MODEL |
| .toString()); |
| } |
| if (map.containsKey(HSProperty.PRECISION_MODEL_SCALE.toString())) { |
| scale = Double.parseDouble(((String) map |
| .get(HSProperty.PRECISION_MODEL_SCALE.toString()))); |
| } |
| if (scale != null && !scale.isNaN() && precisionModelName != null |
| && precisionModelName.equalsIgnoreCase("FIXED")) { |
| return new MGeometryFactory(new PrecisionModel(scale)); |
| } |
| if (precisionModelName == null) { |
| return new MGeometryFactory(); |
| } |
| if (precisionModelName.equalsIgnoreCase("FIXED")) { |
| return new MGeometryFactory( |
| new PrecisionModel(PrecisionModel.FIXED)); |
| } |
| if (precisionModelName.equalsIgnoreCase("FLOATING")) { |
| return new MGeometryFactory(new PrecisionModel( |
| PrecisionModel.FLOATING)); |
| } |
| if (precisionModelName.equalsIgnoreCase("FLOATING_SINGLE")) { |
| return new MGeometryFactory(new PrecisionModel( |
| PrecisionModel.FLOATING_SINGLE)); |
| } |
| logger.warn("Configured for PrecisionModel: " + precisionModelName |
| + " but don't know how to instantiate."); |
| logger.warn("Reverting to default GeometryModel"); |
| return new MGeometryFactory(); |
| } |
| } |
| Property changes: |
| Added: svn:keywords |
| + Id |
| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/Circle.java |
|---|
| 91,18 → 91,27 |
| * If this is the case, we must force the bounding rectangle to be a square. |
| * To this end, we check the box and set the side of the box to the larger |
| * dimension of the rectangle |
| * |
| * @param xLeft |
| * @param yUpper |
| * @param xRight |
| * @param yLower |
| */ |
| public Circle(double xLeft, double yUpper, double xRight, double yLower) { |
| double side = Math.min(Math.abs(xRight - xLeft), Math.abs(yLower |
| - yUpper)); |
| center.x = Math.min(xRight, xLeft) + side / 2; |
| center.y = Math.min(yUpper, yLower) + side / 2; |
| radius = side / 2; |
| this.center.x = Math.min(xRight, xLeft) + side / 2; |
| this.center.y = Math.min(yUpper, yLower) + side / 2; |
| this.radius = side / 2; |
| } |
| /** |
| * Three point method of circle construction. All three points must be on |
| * the circumference of the circle. |
| * |
| * @param point1 |
| * @param point2 |
| * @param point3 |
| */ |
| public Circle(Coordinate point1, Coordinate point2, Coordinate point3) { |
| initThreePointCircle(point1, point2, point3); |
| 111,6 → 120,13 |
| /** |
| * Three point method of circle construction. All three points must be on |
| * the circumference of the circle. |
| * |
| * @param x1 |
| * @param y1 |
| * @param x2 |
| * @param y2 |
| * @param x3 |
| * @param y3 |
| */ |
| public Circle(double x1, double y1, double x2, double y2, double x3, |
| double y3) { |
| 122,16 → 138,16 |
| * shift the center of the circle by delta X and delta Y |
| */ |
| public void shift(double deltaX, double deltaY) { |
| center.x = center.x + deltaX; |
| center.y = center.y + deltaY; |
| this.center.x = this.center.x + deltaX; |
| this.center.y = this.center.y + deltaY; |
| } |
| /** |
| * Move the circle to a new center |
| */ |
| public void move(double x, double y) { |
| center.x = x; |
| center.y = y; |
| this.center.x = x; |
| this.center.y = y; |
| } |
| /** |
| 151,7 → 167,7 |
| Coordinate p3) { |
| double a13, b13, c13; |
| double a23, b23, c23; |
| double x = 0, y = 0, rad = 0; |
| double x = 0., y = 0., rad = 0.; |
| // begin pre-calculations for linear system reduction |
| a13 = 2 * (p1.x - p3.x); |
| 187,11 → 203,11 |
| } |
| public Coordinate getCenter() { |
| return center; |
| return this.center; |
| } |
| public double getRadius() { |
| return radius; |
| return this.radius; |
| } |
| /** |
| 340,26 → 356,28 |
| } |
| public boolean equals(Object o) { |
| if (this == o) |
| if (this == o) { |
| return true; |
| if (o == null || getClass() != o.getClass()) |
| } |
| if (o == null || getClass() != o.getClass()){ |
| return false; |
| } |
| Circle circle = (Circle) o; |
| if (Double.compare(circle.radius, radius) != 0) |
| if (Double.compare(circle.radius, this.radius) != 0){ |
| return false; |
| if (center != null ? !center.equals2D(circle.center) |
| : circle.center != null) |
| } |
| if (this.center != null ? !this.center.equals2D(circle.center) |
| : circle.center != null) { |
| return false; |
| } |
| return true; |
| } |
| public String toString() { |
| return "Circle with Radius = " + radius |
| + " and a center at the coordinates (" + center.x + ", " |
| + center.y + ")"; |
| return "Circle with Radius = " + this.radius |
| + " and a center at the coordinates (" + this.center.x + ", " |
| + this.center.y + ")"; |
| } |
| /** |
| 371,41 → 389,44 |
| * @return The angle of the point from the center of the circle |
| */ |
| public double getAngle(Coordinate p) { |
| double dx = p.x - center.x; |
| double dy = p.y - center.y; |
| double dx = p.x - this.center.x; |
| double dy = p.y - this.center.y; |
| double angle; |
| if (dx == 0.0) { |
| if (dy == 0.0) |
| if (dy == 0.0) { |
| angle = 0.0; |
| else if (dy > 0.0) |
| } else if (dy > 0.0) { |
| angle = Math.PI / 2.0; |
| else |
| } else { |
| angle = (Math.PI * 3.0) / 2.0; |
| } |
| } else if (dy == 0.0) { |
| if (dx > 0.0) |
| if (dx > 0.0) { |
| angle = 0.0; |
| else |
| } else { |
| angle = Math.PI; |
| } |
| } else { |
| if (dx < 0.0) |
| if (dx < 0.0) { |
| angle = Math.atan(dy / dx) + Math.PI; |
| else if (dy < 0.0) |
| } else if (dy < 0.0) { |
| angle = Math.atan(dy / dx) + (2 * Math.PI); |
| else |
| } else { |
| angle = Math.atan(dy / dx); |
| } |
| } |
| return angle; |
| } |
| public Coordinate getPoint(final double angle) { |
| double x = Math.cos(angle) * this.radius; |
| x = x + center.x; |
| x = precisionModel.makePrecise(x); |
| x = x + this.center.x; |
| x = this.precisionModel.makePrecise(x); |
| double y = Math.sin(angle) * this.radius; |
| y = y + center.y; |
| y = precisionModel.makePrecise(y); |
| y = y + this.center.y; |
| y = this.precisionModel.makePrecise(y); |
| return new Coordinate(x, y); |
| } |
| 453,10 → 474,11 |
| * @return the angle between a1 and a2 in the clockwise direction |
| */ |
| public static double subtractAngles(double a1, double a2) { |
| if (a1 < a2) |
| if (a1 < a2) { |
| return a2 - a1; |
| else |
| } else { |
| return TWO_PI - Math.abs(a2 - a1); |
| } |
| } |
| private static final double TWO_PI = Math.PI * 2; |
| 516,7 → 538,7 |
| double diff; |
| if (this.p1.equals2D(this.p2)) { |
| diff = TWO_PI; |
| } else if (clockwise) { |
| } else if (this.clockwise) { |
| diff = this.p1Angle - this.p2Angle; |
| } else { |
| diff = this.p2Angle - this.p1Angle; |
| 536,15 → 558,15 |
| Coordinate chordCenterPt = this.getChordCenterPoint(); |
| double dist = distanceFromCenter(chordCenterPt); |
| if (this.arcAngle > Math.PI) { |
| return radius + dist; |
| return Circle.this.radius + dist; |
| } else { |
| return radius - dist; |
| return Circle.this.radius - dist; |
| } |
| } |
| public Coordinate getChordCenterPoint() { |
| double centerX = p1.x + (p2.x - p1.x) / 2; |
| double centerY = p1.y + (p2.y - p1.y) / 2; |
| double centerX = this.p1.x + (this.p2.x - this.p1.x) / 2; |
| double centerY = this.p1.y + (this.p2.y - this.p1.y) / 2; |
| return new Coordinate(centerX, centerY); |
| } |
| 561,35 → 583,35 |
| } |
| public Coordinate getP1() { |
| return p1; |
| return this.p1; |
| } |
| public Coordinate getP2() { |
| return p2; |
| return this.p2; |
| } |
| public double getArcAngle() { |
| return arcAngle; |
| return this.arcAngle; |
| } |
| public double getArcAngleDegrees() { |
| return Math.toDegrees(arcAngle); |
| return Math.toDegrees(this.arcAngle); |
| } |
| public double getP1Angle() { |
| return p1Angle; |
| return this.p1Angle; |
| } |
| public double getP2Angle() { |
| return p2Angle; |
| return this.p2Angle; |
| } |
| public boolean isClockwise() { |
| return clockwise; |
| return this.clockwise; |
| } |
| public String toString() { |
| return "P1: " + p1 + " P2: " + p2 + " clockwise: " + clockwise; |
| return "P1: " + this.p1 + " P2: " + this.p2 + " clockwise: " + this.clockwise; |
| } |
| } |
| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/HBSpatialExtension.java |
|---|
| 33,12 → 33,14 |
| import java.net.URL; |
| import java.util.Enumeration; |
| import java.util.HashSet; |
| import java.util.Map; |
| import java.util.Set; |
| import org.apache.commons.logging.Log; |
| import org.apache.commons.logging.LogFactory; |
| import org.hibernatespatial.cfg.GeometryFactoryHelper; |
| import org.hibernatespatial.cfg.HSConfiguration; |
| import org.hibernatespatial.helper.PropertyFileReader; |
| import org.hibernatespatial.mgeom.MGeometryFactory; |
| import org.hibernatespatial.spi.SpatialDialectProvider; |
| /** |
| 64,6 → 66,10 |
| private static SpatialDialect defaultSpatialDialect = null; |
| private static final String DIALECT_PROP_NAME = "hibernate.spatial.dialect"; |
| private static HSConfiguration configuration = null; |
| private static MGeometryFactory defaultGeomFactory = new MGeometryFactory(); |
| static { |
| 115,8 → 121,7 |
| search: for (SpatialDialectProvider provider : providers) { |
| for (String dialect : provider.getSupportedDialects()) { |
| if (dialect.equals(dialectProp)) { |
| defaultSpatialDialect = provider.createSpatialDialect( |
| dialectProp, null); |
| defaultSpatialDialect = provider.createSpatialDialect(dialectProp); |
| found = true; |
| break search; |
| } |
| 142,6 → 147,28 |
| */ |
| private HBSpatialExtension() { |
| } |
| public static void setConfiguration(HSConfiguration c){ |
| configuration = c; |
| log.info("Configuring HBSpatialExtensing from " + c.getSource()); |
| //checking for configured dialectname |
| String dialectName = configuration.getDefaultDialect(); |
| if (dialectName != null){ |
| SpatialDialect dialect = createSpatialDialect(dialectName); |
| if (dialect != null){ |
| log.info("Setting Spatial Dialect to : " + dialectName); |
| setDefaultSpatialDialect(dialect); |
| } |
| } |
| //trying to create a defaultGeometryFactory |
| defaultGeomFactory = GeometryFactoryHelper.createGeometryFactory(configuration); |
| log.info("Creating default Geometry Factory"); |
| } |
| public static HSConfiguration getConfiguration(){ |
| return configuration; |
| } |
| /** |
| * @param dialect |
| 154,11 → 181,10 |
| return defaultSpatialDialect; |
| } |
| public static SpatialDialect createSpatialDialect(String dialectName, |
| Map properties) { |
| public static SpatialDialect createSpatialDialect(String dialectName) { |
| SpatialDialect dialect = null; |
| for (SpatialDialectProvider provider : providers) { |
| dialect = provider.createSpatialDialect(dialectName, properties); |
| dialect = provider.createSpatialDialect(dialectName); |
| if (dialect != null) { |
| break; |
| } |
| 170,6 → 196,10 |
| } |
| return dialect; |
| } |
| public static MGeometryFactory getDefaultGeomFactory() { |
| return defaultGeomFactory; |
| } |
| // Helper methods |
| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/AbstractDBGeometryType.java |
|---|
| 38,6 → 38,8 |
| import org.hibernate.HibernateException; |
| import org.hibernate.usertype.ParameterizedType; |
| import org.hibernate.usertype.UserType; |
| import org.hibernatespatial.cfg.GeometryFactoryHelper; |
| import org.hibernatespatial.mgeom.MGeometryFactory; |
| import com.vividsolutions.jts.geom.Geometry; |
| 49,6 → 51,8 |
| */ |
| public abstract class AbstractDBGeometryType implements UserType, |
| ParameterizedType { |
| private MGeometryFactory geomFactory = null; |
| /* |
| * (non-Javadoc) |
| 198,6 → 202,13 |
| * @see org.hibernate.usertype.ParameterizedType#setParameterValues(java.util.Properties) |
| */ |
| public void setParameterValues(Properties parameters) { |
| if (parameters != null){ |
| this.geomFactory = GeometryFactoryHelper.createGeometryFactory(parameters); |
| } |
| } |
| public MGeometryFactory getGeometryFactory(){ |
| return (this.geomFactory == null) ? HBSpatialExtension.getDefaultGeomFactory() : this.geomFactory; |
| } |
| } |
| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/spi/SpatialDialectProvider.java |
|---|
| 51,12 → 51,9 |
| * |
| * @param dialect |
| * Name of the dialect to create. |
| * @param map |
| * A map of properties for use by the provider when creating the |
| * dialect. |
| * @return a SpatialDialect |
| */ |
| public SpatialDialect createSpatialDialect(String dialect, Map map); |
| public SpatialDialect createSpatialDialect(String dialect); |
| /** |
| * Returns the default dialect for this provider. |
| /trunk/hibernate-spatial/src/main/java/org/hibernatespatial/GeometryUserType.java |
|---|
| 52,26 → 52,27 |
| */ |
| public class GeometryUserType implements UserType, ParameterizedType { |
| private Properties properties = null; |
| private SpatialDialect spatialDialect = null; |
| private UserType delegate = null; |
| public static String DIALECT_PARAM_NAME = "dialect"; |
| private void configureDialect() { |
| private void configure(Properties properties) { |
| if (properties == null) { |
| spatialDialect = HBSpatialExtension.getDefaultSpatialDialect(); |
| } else { |
| spatialDialect = HBSpatialExtension.createSpatialDialect(properties |
| .getProperty(DIALECT_PARAM_NAME), properties); |
| .getProperty(DIALECT_PARAM_NAME)); |
| } |
| if (spatialDialect == null) { |
| throw new HibernateSpatialException( |
| "No spatial Dialect could be created"); |
| } |
| delegate = spatialDialect.getGeometryUserType(); |
| if (delegate instanceof ParameterizedType){ |
| ((ParameterizedType)delegate).setParameterValues(properties); |
| } |
| } |
| /** |
| 222,8 → 223,7 |
| } |
| public void setParameterValues(Properties properties) { |
| this.properties = properties; |
| configureDialect(); |
| configure(properties); |
| } |
| } |