[Gislite-admins] SF.net SVN: gislite: [9] trunk
Status: Planning
Brought to you by:
ctomasin
|
From: <cto...@us...> - 2007-08-31 17:52:02
|
Revision: 9
http://gislite.svn.sourceforge.net/gislite/?rev=9&view=rev
Author: ctomasin
Date: 2007-08-31 10:52:04 -0700 (Fri, 31 Aug 2007)
Log Message:
-----------
Added GeomDistance() function and the corrisponding Distance() SQL function
Modified Paths:
--------------
trunk/Makefile
trunk/sqlfunc.c
trunk/wkg.h
trunk/wkgfunc.c
trunk/wkgfunc.h
Modified: trunk/Makefile
===================================================================
--- trunk/Makefile 2007-08-30 18:22:10 UTC (rev 8)
+++ trunk/Makefile 2007-08-31 17:52:04 UTC (rev 9)
@@ -1,16 +1,30 @@
-if test -d /proc ; then
- DYNAMIC= '-shared'
- OPTIONS= ' -lsqlite3 -lm -g'
+#
+# This is not valid in a Makefile
+#
+# if test -d /proc ; then
+# DYNAMIC= '-shared'
+# OPTIONS= ' -lsqlite3 -lm -g'
+# else
+# DYNAMIC= '-fno-common -dynamiclib'
+# OPTIONS= ''
+# fi
+
+#UNAME = Darwin
+UNAME = Linux
+
+ifeq ($(UNAME),Linux)
+ DYNAMIC = -shared
+ OPTIONS = -lsqlite3 -lm -g
else
- DYNAMIC= '-fno-common -dynamiclib'
- OPTIONS= ''
-fi
+ DYNAMIC= -fno-common -dynamiclib
+ OPTIONS=
+endif
NAME = gislite.so
SOURCES = wkgfunc.c sqlfunc.c
OBJS = $(SOURCES:%.c=%.o)
$(NAME) : $(OBJS)
- gcc $(DYNAMIC) $(OPTIONS) -o $@ $(OBJS)
+ gcc $(DYNAMIC) $(OPTIONS) -o $@ $(OBJS)
%.o : %.c
gcc -Wall -O0 -D_GNU_SOURCE -c $<
Modified: trunk/sqlfunc.c
===================================================================
--- trunk/sqlfunc.c 2007-08-30 18:22:10 UTC (rev 8)
+++ trunk/sqlfunc.c 2007-08-31 17:52:04 UTC (rev 9)
@@ -88,7 +88,33 @@
sqlite3_free((void*)pGeom);
}
+/**
+ * SQL function Distance(b)
+ * Returns the distance between two well know binaries as a double
+ * Ex:
+ * sqlite> SELECT AsText(BLOB_POINT) FROM testGeom WHERE Distance(BLOB_POINT,AsBinary('POINT(1.2 1.3 1.4 1.5)')) < 100.0
+ */
+static void Distance(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ WKBGeometry *pGeomA, *pGeomB;
+ double distance;
+ if( sqlite3_value_type(argv[0]) == SQLITE_BLOB && sqlite3_value_type(argv[1]) == SQLITE_BLOB){
+ pGeomA = GeomFromBinary((unsigned char *)sqlite3_value_blob(argv[0]));
+ pGeomB = GeomFromBinary((unsigned char *)sqlite3_value_blob(argv[1]));
+ distance = GeomDistance(pGeomA,pGeomB);
+ if( distance < 0.0 )
+ sqlite3_result_null(context); // if negative means distance can't be evalued
+ sqlite3_result_double(context,distance);
+ } else {
+ sqlite3_result_error(context,messages[6],-1);
+ }
+}
+
+
/**
* SQL function X(b)
* Returns the X-coordinate value for the blob b containing WKBGeometry Point as a double.
@@ -316,7 +342,8 @@
sqlite3_create_function(db, "AsBinary", 1, SQLITE_ANY, 0, AsBinary, 0, 0);
//sqlite3_create_function(db, "SRID", 1, SQLITE_ANY, 0, SRID, 0, 0);
//sqlite3_create_function(db, "Dimension", 1, SQLITE_ANY, 0, Dimension, 0, 0);
-
+ sqlite3_create_function(db, "Distance", 2, SQLITE_ANY, 0,Distance, 0, 0);
+
//Point functions
sqlite3_create_function(db, "X", 1, SQLITE_ANY, 0, X, 0, 0);
sqlite3_create_function(db, "Y", 1, SQLITE_ANY, 0, Y, 0, 0);
Modified: trunk/wkg.h
===================================================================
--- trunk/wkg.h 2007-08-30 18:22:10 UTC (rev 8)
+++ trunk/wkg.h 2007-08-31 17:52:04 UTC (rev 9)
@@ -28,7 +28,7 @@
typedef struct {
uint32 numPoints;
- Point points[1]; // [numPoints] init
+ Point *points; // [numPoints] init
}LinearRing;
enum wkbByteOrder {
@@ -56,42 +56,42 @@
byte byteOrder;
uint32 wkbType; // = 3002;
uint32 numPoints;
- Point points[1]; // init [numPoints]
+ Point *points; // init [numPoints]
}WKBLineString;
typedef struct {
byte byteOrder;
uint32 wkbType; // = 3003;
uint32 numRings;
- LinearRing rings[1]; // init [numRings]
+ LinearRing *rings; // init [numRings]
}WKBPolygon;
typedef struct {
byte byteOrder;
uint32 wkbType; // = 3004;
uint32 num_wkbPoints;
- WKBPoint WKBPoints[1]; // init [num_wkbPoints]
+ WKBPoint *wKBPoints; // init [num_wkbPoints]
}WKBMultiPoint;
typedef struct {
byte byteOrder;
uint32 wkbType; // = 3005;
uint32 num_wkbLineStrings;
- WKBLineString WKBLineStrings[1]; // init [num_wkbLineStrings]
+ WKBLineString *wKBLineStrings; // init [num_wkbLineStrings]
}WKBMultiLineString;
typedef struct {
byte byteOrder;
uint32 wkbType; // = 3006;
uint32 num_wkbPolygons;
- WKBPolygon wkbPolygons[1]; // init [num_wkbPolygons]
+ WKBPolygon *wkbPolygons; // init [num_wkbPolygons]
}WKBMultiPolygon;
typedef struct {
byte byteOrder;
uint32 wkbType; // = 3007;
uint32 num_wkbGeometries;
- char wkbGeometries[1]; // init [num_wkbGeometries];
+ /*WKBGeometry*/ char *wkbGeometries; // init [num_wkbGeometries];
}WKBGeometryCollection;
typedef struct {
@@ -105,4 +105,6 @@
WKBGeometryCollection collection;
};
}WKBGeometry;
+
+
#endif
Modified: trunk/wkgfunc.c
===================================================================
--- trunk/wkgfunc.c 2007-08-30 18:22:10 UTC (rev 8)
+++ trunk/wkgfunc.c 2007-08-31 17:52:04 UTC (rev 9)
@@ -5,19 +5,21 @@
#include <stdio.h>
#include <string.h>
+#include <math.h>
#include "wkgfunc.h"
#include <sqlite3ext.h> // needed by portable sqlite-specific functions
SQLITE_EXTENSION_INIT1 // needed by portable sqlite-specific functions
//char debug[50];
-char *messages[6] = {
+char *messages[7] = {
"ok" // 0
,"unrecognized text type" // 1
,"parse error" // 2
,"unrecognized geometry type" // 3
,"unable to convert from blob" // 4
,"geometry is not a point" // 5
+ ,"wrong parameter type" // 6
};
@@ -210,7 +212,7 @@
*
* @return pointer to unsigned char to be filled
*/
-unsigned char* asBinary(WKBGeometry* pGeom){
+unsigned char* GeomAsBinary(WKBGeometry* pGeom){
int dim;
unsigned char *pBuffer;
unsigned char *pChar;
@@ -248,4 +250,46 @@
return pBuffer;
}
+/**
+ * function GeomDimension(g)
+ * @param pGeom pointer to WKGeometry
+ *
+ * @return number of dimensions of the Geometry
+ */
+int GeomDimension(WKBGeometry *pGeom){
+ int dims;
+ switch(pGeom->point.wkbType){
+ case wkbPoint:
+ dims = 0;
+ break;
+ case wkbLineString:
+ dims = 1;
+ break;
+ default:
+ dims = 0;
+ break;
+ }
+ return dims;
+}
+
+
+/**
+ * function GeomDistance(g)
+ * @param pGeomA pointer to first geometry
+ * @param pGeomB pointer to second geometry
+ *
+ * @return distance between the two geometries
+ */
+double GeomDistance(WKBGeometry *pGeomA,WKBGeometry *pGeomB){
+ double distance;
+ if(pGeomA->point.wkbType == wkbPoint && pGeomB->point.wkbType == wkbPoint){
+ distance = sqrt(pow(pGeomB->point.point.x - pGeomA->point.point.x,2) +
+ pow(pGeomB->point.point.y - pGeomA->point.point.y,2) +
+ pow(pGeomB->point.point.z - pGeomA->point.point.z,2) +
+ pow(pGeomB->point.point.m - pGeomA->point.point.m,2));
+ }else{
+ distance = -1.0; // not able to measure
+ }
+ return distance;
+}
Modified: trunk/wkgfunc.h
===================================================================
--- trunk/wkgfunc.h 2007-08-30 18:22:10 UTC (rev 8)
+++ trunk/wkgfunc.h 2007-08-31 17:52:04 UTC (rev 9)
@@ -19,6 +19,7 @@
unsigned char* GeomAsBinary(WKBGeometry* pGeom);
+double GeomDistance(WKBGeometry *pGeomA,WKBGeometry *pGeomB);
extern char *messages[];
#endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|