[Gislite-admins] SF.net SVN: gislite: [8] trunk
Status: Planning
Brought to you by:
ctomasin
|
From: <cto...@us...> - 2007-08-30 18:22:09
|
Revision: 8
http://gislite.svn.sourceforge.net/gislite/?rev=8&view=rev
Author: ctomasin
Date: 2007-08-30 11:22:10 -0700 (Thu, 30 Aug 2007)
Log Message:
-----------
Some modify by Domenico committed by Carlo
Modified Paths:
--------------
trunk/Makefile
trunk/sqlfunc.c
trunk/wkgfunc.c
trunk/wkgfunc.h
Modified: trunk/Makefile
===================================================================
--- trunk/Makefile 2007-08-24 15:20:12 UTC (rev 7)
+++ trunk/Makefile 2007-08-30 18:22:10 UTC (rev 8)
@@ -1,3 +1,11 @@
+if test -d /proc ; then
+ DYNAMIC= '-shared'
+ OPTIONS= ' -lsqlite3 -lm -g'
+else
+ DYNAMIC= '-fno-common -dynamiclib'
+ OPTIONS= ''
+fi
+
NAME = gislite.so
SOURCES = wkgfunc.c sqlfunc.c
OBJS = $(SOURCES:%.c=%.o)
@@ -2,3 +10,3 @@
$(NAME) : $(OBJS)
- gcc -shared -lsqlite3 -lm -g -o $@ $(OBJS)
+ gcc $(DYNAMIC) $(OPTIONS) -o $@ $(OBJS)
Modified: trunk/sqlfunc.c
===================================================================
--- trunk/sqlfunc.c 2007-08-24 15:20:12 UTC (rev 7)
+++ trunk/sqlfunc.c 2007-08-30 18:22:10 UTC (rev 8)
@@ -7,95 +7,85 @@
#include <string.h>
#include <sqlite3ext.h>
#include "wkgfunc.h"
+
SQLITE_EXTENSION_INIT1
/**
- * SQL function toWKT(b)
+ * SQL function AsText(b)
* Returns a WKT openGis Geometry representation from a sqlite blob b.
* Ex:
- * sqlite> SELECT (toWKT(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
+ * sqlite> SELECT (AsText(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
* sqlite> POINT(1.1 1.2 1.3 4.4)
*
* @param argv blob well-known binary to sqlite by its own function
*
* @return string geometry as well-known text
*/
-static void ToWKT(
+static void AsText(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
unsigned char *pBlob;
- int error;
char *pString;
- WKBGeometry Geom;
+ WKBGeometry *pGeom;
//check for null values
if(sqlite3_value_type(argv[0]) == SQLITE_NULL) return;
//only if value type is BLOB
- switch( sqlite3_value_type(argv[0])){
- case SQLITE_BLOB:
+ if( sqlite3_value_type(argv[0]) == SQLITE_BLOB){
//get blob from argv
pBlob = (unsigned char*) sqlite3_value_blob(argv[0]);
//fill structure from blob
- error = 0;
- error = fromBinary(pBlob, &Geom);
- if(error) sqlite3_result_error(context, messages[error], -1);
+ pGeom = (WKBGeometry*)GeomFromBinary(pBlob);
//convert to string
- pString = asText(&Geom);
- break;
- default:
+ pString = (char*)GeomAsText(pGeom);
+ }else{
sqlite3_result_error(context, messages[4], -1);
}
//write out results
sqlite3_result_text(context, pString, -1, SQLITE_TRANSIENT);
-
//free AsText allocated memory
- sqlite3_free(pString);
+ sqlite3_free((void*)pString);
+ sqlite3_free((void*)pGeom);
}
/**
- * SQL function toWKB(t)
+ * SQL function AsBinary(t)
* Returns a WKB blob from a WKT openGis Geometry representation t
* Ex:
- * sqlite> INSERT INTO testGeom (BLOB_POINT) VALUES (toWKB('POINT(1.2 1.3 1.4 1.5)'));
+ * sqlite> INSERT INTO testGeom (BLOB_POINT) VALUES (AsBinary('POINT(1.2 1.3 1.4 1.5)'));
*
* @param argv string geometry as well-known text
*
* @return blob well-known binary to sqlite by its own function
*/
-static void ToWKB(
+static void AsBinary(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
- int error, sizeBin;
+ int sizeBin;
char *pBuf;
unsigned char *pBin;
- WKBGeometry Geom;
+ WKBGeometry *pGeom;
//check for null values
if(sqlite3_value_type(argv[0]) == SQLITE_NULL) return;
- //assign parameter
+ //assign sqlite parameter
pBuf = (char*)sqlite3_value_text(argv[0]);
-
- error = 0;
- error = fromText(pBuf, &Geom);
- if(error) sqlite3_result_error(context, messages[error], -1);
+ //read WKT into geometry
+ pGeom = (WKBGeometry*)GeomFromText(pBuf);
//fill char array
- pBin = asBinary(&Geom);
-//double tmp;
-//memcpy(&tmp, pBin+sizeof(byte)+sizeof(uint32)+sizeof(double),sizeof(double));
-//sprintf(debug,"bin: %lf",tmp);
-//sqlite3_result_error(context, debug, -1);
-
- //sizeBin = strlen((char*)pBin);
- sizeBin = sizeof(WKBGeometry);
+ pBin = (unsigned char*)GeomAsBinary(pGeom);
+ //measure binary buffer as char array
+ sizeBin = strlen((char*)pBin)+1;
+ //assign binary buffer
sqlite3_result_blob(context, pBin, sizeBin, SQLITE_TRANSIENT);
-
- //free memory allocated by asBinary()
+ //free allocated memory
sqlite3_free((void*)pBin);
+ sqlite3_free((void*)pGeom);
}
@@ -115,15 +105,15 @@
int argc,
sqlite3_value **argv
){
- WKBGeometry Geom;
+ WKBGeometry *pGeom;
//only if value type is BLOB
if( sqlite3_value_type(argv[0]) == SQLITE_BLOB){
//get blob from argv and fill structure from blob
- fromBinary((unsigned char*) sqlite3_value_blob(argv[0]), &Geom);
+ pGeom = (WKBGeometry*)GeomFromBinary((unsigned char*) sqlite3_value_blob(argv[0]));
//return coordinate
- if(Geom.point.wkbType == 3001)
- sqlite3_result_double(context, Geom.point.point.x);
+ if(pGeom->point.wkbType == 3001)
+ sqlite3_result_double(context, pGeom->point.point.x);
else
sqlite3_result_error(context, messages[5], -1);
}else{
@@ -148,15 +138,15 @@
int argc,
sqlite3_value **argv
){
- WKBGeometry Geom;
+ WKBGeometry *pGeom;
//only if value type is BLOB
if( sqlite3_value_type(argv[0]) == SQLITE_BLOB){
//get blob from argv and fill structure from blob
- fromBinary((unsigned char*) sqlite3_value_blob(argv[0]), &Geom);
+ pGeom = (WKBGeometry*)GeomFromBinary((unsigned char*) sqlite3_value_blob(argv[0]));
//return coordinate
- if(Geom.point.wkbType == 3001)
- sqlite3_result_double(context, Geom.point.point.y);
+ if(pGeom->point.wkbType == 3001)
+ sqlite3_result_double(context, pGeom->point.point.y);
else
sqlite3_result_error(context, messages[5], -1);
}else{
@@ -180,15 +170,15 @@
int argc,
sqlite3_value **argv
){
- WKBGeometry Geom;
+ WKBGeometry *pGeom;
//only if value type is BLOB
if( sqlite3_value_type(argv[0]) == SQLITE_BLOB){
//get blob from argv and fill structure from blob
- fromBinary((unsigned char*) sqlite3_value_blob(argv[0]), &Geom);
+ pGeom = (WKBGeometry*)GeomFromBinary((unsigned char*) sqlite3_value_blob(argv[0]));
//return coordinate
- if(Geom.point.wkbType == 3001)
- sqlite3_result_double(context, Geom.point.point.z);
+ if(pGeom->point.wkbType == 3001)
+ sqlite3_result_double(context, pGeom->point.point.z);
else
sqlite3_result_error(context, messages[5], -1);
}else{
@@ -213,15 +203,15 @@
int argc,
sqlite3_value **argv
){
- WKBGeometry Geom;
+ WKBGeometry *pGeom;
//only if value type is BLOB
if( sqlite3_value_type(argv[0]) == SQLITE_BLOB){
//get blob from argv and fill structure from blob
- fromBinary((unsigned char*) sqlite3_value_blob(argv[0]), &Geom);
+ pGeom = (WKBGeometry*)GeomFromBinary((unsigned char*) sqlite3_value_blob(argv[0]));
//return coordinate
- if(Geom.point.wkbType == 3001)
- sqlite3_result_double(context, Geom.point.point.m);
+ if(pGeom->point.wkbType == 3001)
+ sqlite3_result_double(context, pGeom->point.point.m);
else
sqlite3_result_error(context, messages[5], -1);
}else{
@@ -234,7 +224,7 @@
* SQL function StartPoint(g)
* Returns the start Point of a WKBGeometry LineString g.
* Ex:
- * sqlite> SELECT AsText(StartPoint(GeomfromBinary(BLOB_LINE))) FROM testGeom WHERE INDEX=1;
+ * sqlite> SELECT AsText(StartPoint(BLOB_LINE)) FROM testGeom WHERE INDEX=1;
* sqlite> POINT(1.1 1.2 1.3 4.4)
*
* @param g WKBGeometry structure containing LineString
@@ -247,7 +237,7 @@
* SQL function EndPoint(g)
* Returns the end Point of a LineString.
* Ex:
- * sqlite> SELECT AsText(EndPoint(GeomfromBinary(BLOB_LINE))) FROM testGeom WHERE INDEX=1;
+ * sqlite> SELECT AsText(EndPoint(BLOB_LINE)) FROM testGeom WHERE INDEX=1;
* sqlite> POINT(5.1 5.2 5.3 4.4)
*
* @param g WKBGeometry structure containing LineString
@@ -260,7 +250,7 @@
* SQL function Length(g)
* Returns the length of the LineString geometry g as double.
* Ex:
- * sqlite> SELECT Length(GeomfromBinary(BLOB_LINE)) FROM testGeom WHERE INDEX=1;
+ * sqlite> SELECT Length(BLOB_LINE) FROM testGeom WHERE INDEX=1;
* sqlite> 5.8926358638273
*
* @param g WKBGeometry structure containing LineString
@@ -273,7 +263,7 @@
* SQL function NumPoints(g)
* Returns the number of points in the WKBGeometry LineString g.
* Ex:
- * sqlite> SELECT NumPoints(GeomfromBinary(BLOB_LINE)) FROM testGeom WHERE INDEX=1;
+ * sqlite> SELECT NumPoints(BLOB_LINE) FROM testGeom WHERE INDEX=1;
* sqlite> 12
*
* @param g WKBGeometry structure containing LineString
@@ -286,7 +276,7 @@
* SQL function PointN(g, n)
* Returns the n-th point in the WKBGeometry Linestring g. Point numbers begin at 0.
* Ex:
- * sqlite> SELECT AsText(PointN(GeomfromBinary(BLOB_LINE),10)) FROM testGeom WHERE INDEX=1;
+ * sqlite> SELECT AsText(PointN(BLOB_LINE,10)) FROM testGeom WHERE INDEX=1;
* sqlite> POINT(1.1 1.2 0.0 0.0)
*
* @param g WKBGeometry structure containing LineString
@@ -301,7 +291,7 @@
* Returns 1 if the WKBGeometry LineString g is closed and is simple.
* Returns 0 if ls is not a ring, and -1 if it is NULL.
* Ex:
- * sqlite> SELECT IsRing(GeomfromBinary(BLOB_LINE)) FROM testGeom WHERE INDEX=1;
+ * sqlite> SELECT IsRing(BLOB_LINE) FROM testGeom WHERE INDEX=1;
* sqlite> 1
*
* @param g WKBGeometry structure containing LineString
@@ -320,8 +310,14 @@
const sqlite3_api_routines *pApi
){
SQLITE_EXTENSION_INIT2(pApi)
- sqlite3_create_function(db, "toWKT", 1, SQLITE_ANY, 0, ToWKT, 0, 0);
- sqlite3_create_function(db, "toWKB", 1, SQLITE_ANY, 0, ToWKB, 0, 0);
+
+ //Geometry functions
+ sqlite3_create_function(db, "AsText", 1, SQLITE_ANY, 0, AsText, 0, 0);
+ 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);
+
+ //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);
sqlite3_create_function(db, "Z", 1, SQLITE_ANY, 0, Z, 0, 0);
Modified: trunk/wkgfunc.c
===================================================================
--- trunk/wkgfunc.c 2007-08-24 15:20:12 UTC (rev 7)
+++ trunk/wkgfunc.c 2007-08-30 18:22:10 UTC (rev 8)
@@ -39,24 +39,26 @@
swap_char(i+3, i);
swap_char(i+2, i+1);
}
+
+
/**
- * function FromText
+ * function GeomFromText(t)
* @param pWKT pointer to string in WKT encoding
- * @param pGeom pointer to WKGeometry to be filled
*
- * @return integer error code
+ * @return pointer to WKGeometry
*/
-int fromText(char *pWKT, WKBGeometry *pGeom){
+WKBGeometry* GeomFromText(char *pWKT){
int len, i;
char *pType;
char *pContent;
+ WKBGeometry *pGeom;
//parse string passed as parameter
//search first '('
len = strlen(pWKT);
for (i=0; pWKT[i]!='(' && i<=len; ++i) ;
//test search result
- if(i<5 || i>=len) return 1;// "unrecognized text type"
+ if(i<5 || i>=len) return NULL;// "unrecognized text type"
//copy geometry type
pType = (char*)sqlite3_malloc((int)i+1);
strncpy(pType, pWKT, i);
@@ -67,6 +69,7 @@
//assigning structure to blob
//POINT(1.1 1.2 1.3 1.4)
if(strncmp(pType, "POINT", 5)==0){
+ pGeom = (WKBGeometry*) sqlite3_malloc(sizeof(WKBPoint));
//fill geometry as point
pGeom->point.byteOrder = (byte)'1';//little endian ... autoconf endianness
pGeom->point.wkbType = (uint32)3001;
@@ -79,8 +82,8 @@
//free all allocated memory
sqlite3_free((void*)pType);
sqlite3_free((void*)pContent);
- return 2;//"parse error"
}
+ return pGeom;
}
//LINESTRING(1 1 1 1, 2 2 2 2, 3 3 3 3)
@@ -98,25 +101,24 @@
//free all allocated memory
sqlite3_free((void*)pType);
sqlite3_free((void*)pContent);
- return 3;// "unrecognized geometry type"
+ return NULL;// "unrecognized geometry type"
}
//free all allocated memory
sqlite3_free((void*)pType);
sqlite3_free((void*)pContent);
- return 0;
+ return NULL;
}
/**
- * function AsText
+ * function GeomAsText(g)
* @param pGeom pointer to WKGeometry to be encoded
- * @param pWKT pointer to string containing the text rapresentation created by the function
*
- * @return as pWKT
+ * @return pointer to string
*/
-char* asText(WKBGeometry *pGeom){
- int len;
+char* GeomAsText(WKBGeometry *pGeom){
+ //int len;
char *pWKT;
//char *sBuffer;
@@ -124,7 +126,7 @@
switch(pGeom->point.wkbType){
case 3001: // wkbPoint
//POINT(1.200000 1.300000 1.400000 1.500000)
- len = asprintf(&pWKT, "POINT(%lf %lf %lf %lf)"
+ pWKT = sqlite3_mprintf("POINT(%lf %lf %lf %lf)"
, pGeom->point.point.x
, pGeom->point.point.y
, pGeom->point.point.z
@@ -142,7 +144,7 @@
//GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
default:
- asprintf(&pWKT, "Error: %s", messages[4]);
+ pWKT = sqlite3_mprintf("Error: %s", messages[4]);
}
return pWKT;
@@ -150,16 +152,17 @@
/**
- * function FromBinary
- * @param pBin pointer to unsigned char to be read
- * @param pGeom pointer to WKGeometry to be filled
+ * function GeomFromBinary(b)
+ * @param pointer to unsigned char b blob to be read
*
- * @return integer error code
+ * @return pointer to WKGeometry to be filled
*/
-int fromBinary(unsigned char *pBin, WKBGeometry *pGeom){
+WKBGeometry* GeomFromBinary(unsigned char *pBin){
int bBigendian;
uint32 wkbType;
- int size, sizedbl=sizeof(double);
+ int size;
+ int sizedbl = sizeof(double);
+ WKBGeometry *pGeom;
//determining endianness from first byte
if (pBin[0] == '0') bBigendian = 1; // Big endian
@@ -172,6 +175,9 @@
//set Geometry by wkbType
switch(wkbType){
case 3001: // wkbPoint
+ //alloc WKBGeometry for Point size
+ pGeom = (WKBGeometry*) sqlite3_malloc(sizeof(WKBPoint));
+ //assign order-first byte and type
pGeom->point.byteOrder = pBin[0];
pGeom->point.wkbType = wkbType;
//read coordinates from blob into WKBGeometry
@@ -183,21 +189,23 @@
memcpy(&pGeom->point.point.z, pBin+size, sizedbl);
size += sizedbl;
memcpy(&pGeom->point.point.m, pBin+size, sizedbl);
+ //return filled pointer
+ return pGeom;
break;
case 3002: // wkbLineString
break;
default:
- return 4;// "unable to convert from blob"
+ return NULL;// "unable to convert from blob"
}
- return 0;
+ return NULL;
}
/**
- * function AsBinary
+ * function GeomAsBinary(g)
* @param pGeom pointer to WKGeometry to be converted
*
* @return pointer to unsigned char to be filled
Modified: trunk/wkgfunc.h
===================================================================
--- trunk/wkgfunc.h 2007-08-24 15:20:12 UTC (rev 7)
+++ trunk/wkgfunc.h 2007-08-30 18:22:10 UTC (rev 8)
@@ -8,13 +8,17 @@
void flip_endian_int32(char *i);
-int fromText(char *pWKT, WKBGeometry *pGeom);
-char* asText(WKBGeometry *pGeom);
+//Geometry function declarations
-int fromBinary(unsigned char *pBin, WKBGeometry *pGeom);
+WKBGeometry* GeomFromText(char *pWKT);
-unsigned char* asBinary(WKBGeometry* pGeom);
+char* GeomAsText(WKBGeometry *pGeom);
+WKBGeometry* GeomFromBinary(unsigned char *pBin);
+
+unsigned char* GeomAsBinary(WKBGeometry* pGeom);
+
+
extern char *messages[];
#endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|