Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 71 → Rev 64

/trunk/hibernate-spatial/src/main/java/org/hibernatespatial/Circle.java
91,27 → 91,18
* 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));
this.center.x = Math.min(xRight, xLeft) + side / 2;
this.center.y = Math.min(yUpper, yLower) + side / 2;
this.radius = side / 2;
center.x = Math.min(xRight, xLeft) + side / 2;
center.y = Math.min(yUpper, yLower) + side / 2;
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);
120,13 → 111,6
/**
* 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) {
138,16 → 122,16
* shift the center of the circle by delta X and delta Y
*/
public void shift(double deltaX, double deltaY) {
this.center.x = this.center.x + deltaX;
this.center.y = this.center.y + deltaY;
center.x = center.x + deltaX;
center.y = center.y + deltaY;
}
 
/**
* Move the circle to a new center
*/
public void move(double x, double y) {
this.center.x = x;
this.center.y = y;
center.x = x;
center.y = y;
}
 
/**
167,7 → 151,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);
203,11 → 187,11
}
 
public Coordinate getCenter() {
return this.center;
return center;
}
 
public double getRadius() {
return this.radius;
return radius;
}
 
/**
356,28 → 340,26
}
 
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, this.radius) != 0){
if (Double.compare(circle.radius, radius) != 0)
return false;
}
if (this.center != null ? !this.center.equals2D(circle.center)
: circle.center != null) {
if (center != null ? !center.equals2D(circle.center)
: circle.center != null)
return false;
}
 
return true;
}
 
public String toString() {
return "Circle with Radius = " + this.radius
+ " and a center at the coordinates (" + this.center.x + ", "
+ this.center.y + ")";
return "Circle with Radius = " + radius
+ " and a center at the coordinates (" + center.x + ", "
+ center.y + ")";
}
 
/**
389,44 → 371,41
* @return The angle of the point from the center of the circle
*/
public double getAngle(Coordinate p) {
double dx = p.x - this.center.x;
double dy = p.y - this.center.y;
double dx = p.x - center.x;
double dy = p.y - 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 + this.center.x;
x = this.precisionModel.makePrecise(x);
x = x + center.x;
x = precisionModel.makePrecise(x);
 
double y = Math.sin(angle) * this.radius;
y = y + this.center.y;
y = this.precisionModel.makePrecise(y);
y = y + center.y;
y = precisionModel.makePrecise(y);
return new Coordinate(x, y);
}
 
474,11 → 453,10
* @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;
538,7 → 516,7
double diff;
if (this.p1.equals2D(this.p2)) {
diff = TWO_PI;
} else if (this.clockwise) {
} else if (clockwise) {
diff = this.p1Angle - this.p2Angle;
} else {
diff = this.p2Angle - this.p1Angle;
558,15 → 536,15
Coordinate chordCenterPt = this.getChordCenterPoint();
double dist = distanceFromCenter(chordCenterPt);
if (this.arcAngle > Math.PI) {
return Circle.this.radius + dist;
return radius + dist;
} else {
return Circle.this.radius - dist;
return radius - dist;
}
}
 
public Coordinate getChordCenterPoint() {
double centerX = this.p1.x + (this.p2.x - this.p1.x) / 2;
double centerY = this.p1.y + (this.p2.y - this.p1.y) / 2;
double centerX = p1.x + (p2.x - p1.x) / 2;
double centerY = p1.y + (p2.y - p1.y) / 2;
return new Coordinate(centerX, centerY);
}
 
583,35 → 561,35
}
 
public Coordinate getP1() {
return this.p1;
return p1;
}
 
public Coordinate getP2() {
return this.p2;
return p2;
}
 
public double getArcAngle() {
return this.arcAngle;
return arcAngle;
}
 
public double getArcAngleDegrees() {
return Math.toDegrees(this.arcAngle);
return Math.toDegrees(arcAngle);
}
 
public double getP1Angle() {
return this.p1Angle;
return p1Angle;
}
 
public double getP2Angle() {
return this.p2Angle;
return p2Angle;
}
 
public boolean isClockwise() {
return this.clockwise;
return clockwise;
}
 
public String toString() {
return "P1: " + this.p1 + " P2: " + this.p2 + " clockwise: " + this.clockwise;
return "P1: " + p1 + " P2: " + p2 + " clockwise: " + clockwise;
}
}
 
/trunk/hibernate-spatial/src/main/java/org/hibernatespatial/HBSpatialExtension.java
33,14 → 33,12
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;
 
/**
66,10 → 64,6
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 {
 
121,7 → 115,8
search: for (SpatialDialectProvider provider : providers) {
for (String dialect : provider.getSupportedDialects()) {
if (dialect.equals(dialectProp)) {
defaultSpatialDialect = provider.createSpatialDialect(dialectProp);
defaultSpatialDialect = provider.createSpatialDialect(
dialectProp, null);
found = true;
break search;
}
147,28 → 142,6
*/
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
181,10 → 154,11
return defaultSpatialDialect;
}
 
public static SpatialDialect createSpatialDialect(String dialectName) {
public static SpatialDialect createSpatialDialect(String dialectName,
Map properties) {
SpatialDialect dialect = null;
for (SpatialDialectProvider provider : providers) {
dialect = provider.createSpatialDialect(dialectName);
dialect = provider.createSpatialDialect(dialectName, properties);
if (dialect != null) {
break;
}
196,10 → 170,6
}
return dialect;
}
public static MGeometryFactory getDefaultGeomFactory() {
return defaultGeomFactory;
}
 
// Helper methods
 
/trunk/hibernate-spatial/src/main/java/org/hibernatespatial/AbstractDBGeometryType.java
38,8 → 38,6
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;
 
51,8 → 49,6
*/
public abstract class AbstractDBGeometryType implements UserType,
ParameterizedType {
private MGeometryFactory geomFactory = null;
 
/*
* (non-Javadoc)
202,13 → 198,6
* @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,9 → 51,12
*
* @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);
public SpatialDialect createSpatialDialect(String dialect, Map map);
 
/**
* Returns the default dialect for this provider.
/trunk/hibernate-spatial/src/main/java/org/hibernatespatial/GeometryUserType.java
52,27 → 52,26
*/
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 configure(Properties properties) {
private void configureDialect() {
if (properties == null) {
spatialDialect = HBSpatialExtension.getDefaultSpatialDialect();
} else {
spatialDialect = HBSpatialExtension.createSpatialDialect(properties
.getProperty(DIALECT_PARAM_NAME));
.getProperty(DIALECT_PARAM_NAME), properties);
}
if (spatialDialect == null) {
throw new HibernateSpatialException(
"No spatial Dialect could be created");
}
delegate = spatialDialect.getGeometryUserType();
if (delegate instanceof ParameterizedType){
((ParameterizedType)delegate).setParameterValues(properties);
}
}
 
/**
223,7 → 222,8
}
 
public void setParameterValues(Properties properties) {
configure(properties);
this.properties = properties;
configureDialect();
}
 
}