[Gislite-admins] SF.net SVN: gislite: [3] trunk
Status: Planning
Brought to you by:
ctomasin
|
From: <cto...@us...> - 2007-08-23 21:25:34
|
Revision: 3
http://gislite.svn.sourceforge.net/gislite/?rev=3&view=rev
Author: ctomasin
Date: 2007-08-23 14:25:37 -0700 (Thu, 23 Aug 2007)
Log Message:
-----------
The file gislite.c has been splitted into sqlfunc.c and wkgfunc.c to reflect the distinction between WGK management C functions (wkgfunc.c) and SQL callable functions (sqlfunc.c)
Added Paths:
-----------
trunk/sqlfunc.c
trunk/wkgfunc.c
trunk/wkgfunc.h
Removed Paths:
-------------
trunk/gislite.c
Deleted: trunk/gislite.c
===================================================================
--- trunk/gislite.c 2007-08-23 19:44:42 UTC (rev 2)
+++ trunk/gislite.c 2007-08-23 21:25:37 UTC (rev 3)
@@ -1,484 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-//#include <stdlib.h>
-#include <sqlite3ext.h>
-#include "wkg.h"
-
-
-SQLITE_EXTENSION_INIT1
-
-char *messages[5] = {
- "ok"
- ,"unrecognized text type"
- ,"parse error"
- ,"unrecognized geometry type"
- ,"unable to convert from blob"
-};
-
-//char debug[50];
-
-void swap_char(char *a,char *b){
- char c;
- c = *a;
- *a=*b;
- *b=c;
-}
-
-void flip_endian_double(char *d){
- swap_char(d+7, d);
- swap_char(d+6, d+1);
- swap_char(d+5, d+2);
- swap_char(d+4, d+3);
-}
-
-void flip_endian_int32(char *i){
- swap_char(i+3, i);
- swap_char(i+2, i+1);
-}
-
-
-/**
- * function AsText
- * @param pointer to WKGeometry to be encoded
- *
- * @return pointer to string to be filled
- */
-char* AsText(WKBGeometry *pGeom){
- int len, i;
- char *pWKT;
- //char *sBuffer;
-
- //create string by wkbType
- switch(pGeom->point.wkbType){
- case 3001: // wkbPoint
- //POINT(1.200000 1.300000 1.400000 1.500000)
- len = asprintf(&pWKT, "POINT(%lf %lf %lf %lf)"
- , pGeom->point.point.x
- , pGeom->point.point.y
- , pGeom->point.point.z
- , pGeom->point.point.m);
- break;
-
- case 3002: // wkbLineString
- //LINESTRING(0 0, 10 10, 20 25, 50 60)
- break;
-
- //POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
- //MULTIPOINT(0 0, 20 20, 60 60)
- //MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
- //MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
- //GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
-
- default:
- asprintf(&pWKT, "Error: %s", messages[4]);
- }
-
- return pWKT;
-}
-
-
-/**
- * function FromBinary
- * @param pointer to unsigned char to be read
- * @param pointer to WKGeometry to be filled
- *
- * @return integer error code
- */
-int FromBinary(unsigned char *pBin, WKBGeometry *pGeom){
- int bBigendian;
- uint32 wkbType;
- int size, sizedbl=sizeof(double);
-
- //determining endianness from first byte
- if (pBin[0] == '0') bBigendian = 1; // Big endian
- else bBigendian = 0; // Little endian
-
- //type
- memcpy(&wkbType, pBin+1, sizeof(uint32));
- //if(bBigendian) flip_endian_int32((char*)&wkbType);
-
- //set Geometry by wkbType
- switch(wkbType){
- case 3001: // wkbPoint
- pGeom->point.byteOrder = pBin[0];
- pGeom->point.wkbType = wkbType;
- //read coordinates from blob into WKBGeometry
- size = sizeof(byte) + sizeof(uint32);
- memcpy(&pGeom->point.point.x, pBin+size, sizedbl);
- size += sizedbl;
- memcpy(&pGeom->point.point.y, pBin+size, sizedbl);
- size += sizedbl;
- memcpy(&pGeom->point.point.z, pBin+size, sizedbl);
- size += sizedbl;
- memcpy(&pGeom->point.point.m, pBin+size, sizedbl);
- break;
-
- case 3002: // wkbLineString
- break;
-
- default:
- return 4;// "unable to convert from blob"
- }
-
- return 0;
-}
-
-
-
-/**
- * SQL function toWKT(b)
- * Returns a WKT openGis Geometry representation from a sqlite blob b.
- * Ex:
- * sqlite> SELECT (toWKT(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
- * sqlite> POINT(1.1 1.2 1.3 4.4)
- *
- * @param blob well-known binary to sqlite by its own function
- *
- * @return string geometry as well-known text
- */
-static void toWKT(
- sqlite3_context *context,
- int argc,
- sqlite3_value **argv
-){
- unsigned char *pBlob;
- int error;
- char *pString;
- WKBGeometry Geom;
-
- //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:
- //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);
- //convert to string
- pString = AsText(&Geom);
- break;
- default:
- sqlite3_result_error(context, messages[4], -1);
- }
-
- //write out results
- sqlite3_result_text(context, pString, -1, SQLITE_TRANSIENT);
-
- //free AsText allocated memory
- free(pString);
-}
-
-
-/**
- * function FromText
- * @param pointer to string in WKT encoding
- * @param pointer to WKGeometry to be filled
- *
- * @return integer error code
- */
-int FromText(char *pWKT, WKBGeometry *pGeom){
- int len, i;
- char *pType;
- char *pContent;
-
- //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"
- //copy geometry type
- pType = (char*)sqlite3_malloc((int)i+1);
- strncpy(pType, pWKT, i);
- //copy geometry content
- pContent = (char*)sqlite3_malloc((int)len-i);
- strncpy(pContent, pWKT+i, len-(i-1));
-
- //assigning structure to blob
- //POINT(1.1 1.2 1.3 1.4)
- if(strncmp(pType, "POINT", 5)==0){
- //fill geometry as point
- pGeom->point.byteOrder = (byte)'1';//little endian ... autoconf endianness
- pGeom->point.wkbType = (uint32)3001;
- if(sscanf(pContent+1, "%lf %lf %lf %lf",
- &pGeom->point.point.x,
- &pGeom->point.point.y,
- &pGeom->point.point.z,
- &pGeom->point.point.m)
- !=4){
- //free all allocated memory
- sqlite3_free((void*)pType);
- sqlite3_free((void*)pContent);
- return 2;//"parse error"
- }
- }
-
- //LINESTRING(1 1 1 1, 2 2 2 2, 3 3 3 3)
- else if(strncmp(pWKT,"LINESTRING", 10)==0){
- }
-
- //POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
- //MULTIPOINT(0 0, 20 20, 60 60)
- //MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
- //MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
- //GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
-
- //ERROR
- else{
- //free all allocated memory
- sqlite3_free((void*)pType);
- sqlite3_free((void*)pContent);
- return 3;// "unrecognized geometry type"
- }
-
- //free all allocated memory
- sqlite3_free((void*)pType);
- sqlite3_free((void*)pContent);
- return 0;
-}
-
-
-/**
- * function AsBinary
- * @param pointer to WKGeometry to be converted
- *
- * @return pointer to unsigned char to be filled
- */
-unsigned char* AsBinary(WKBGeometry* pGeom){
- int dim, i;
- unsigned char *pBuffer;
- unsigned char *pChar;
-
- //alloc by wkbType
- switch(pGeom->point.wkbType){
- case 3001: // wkbPoint
- dim = sizeof(WKBPoint);
- pBuffer = (unsigned char*)sqlite3_malloc(dim);
- memset(pBuffer, 0, dim);
- pChar = pBuffer;
- //fill Geometry
- memcpy(pChar, (unsigned char*)&pGeom->point.byteOrder, sizeof(byte));
- pChar += sizeof(byte);
- memcpy(pChar, &pGeom->point.wkbType, sizeof(uint32));
- pChar += sizeof(uint32);
- memcpy(pChar, &pGeom->point.point.x, sizeof(double));
- pChar += sizeof(double);
- memcpy(pChar, &pGeom->point.point.y, sizeof(double));
- pChar += sizeof(double);
- memcpy(pChar, &pGeom->point.point.z, sizeof(double));
- pChar += sizeof(double);
- memcpy(pChar, &pGeom->point.point.m, sizeof(double));
- break;
-
- case 3002: // wkbLineString
- //sprintf(debug,"2");
- break;
-
- default:
- pBuffer = NULL;// "unrecognized geometry type"
- //to be tested outside
- }
-
- return pBuffer;
-}
-
-
-/**
- * SQL function toWKB(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)'));
- *
- * @param string geometry as well-known text
- *
- * @return blob well-known binary to sqlite by its own function
- */
-static void toWKB(
- sqlite3_context *context,
- int argc,
- sqlite3_value **argv
-){
- int error, sizeBin;
- char *pBuf;
- unsigned char *pBin;
- WKBGeometry Geom;
-
- //check for null values
- if(sqlite3_value_type(argv[0]) == SQLITE_NULL) return;
- //assign parameter
- pBuf = (char*)sqlite3_value_text(argv[0]);
-
- error = 0;
- error = FromText(pBuf, &Geom);
- if(error) sqlite3_result_error(context, messages[error], -1);
- //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);
- sqlite3_result_blob(context, pBin, sizeBin, SQLITE_TRANSIENT);
-
- //free memory allocated by asBinary()
- sqlite3_free((void*)pBin);
-}
-
-
-/**
- * SQL function X(g)
- * Returns the X-coordinate value for the WKBGeometry Point g as a double.
- * Ex:
- * sqlite> SELECT X(GeomFromBinary(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
- * sqlite> 5.8926358638273
- *
- * @param WKBGeometry structure g containing Point
- *
- * @return double x coordinate
- */
-
-
-/**
- * SQL function Y(g)
- * Returns the Y-coordinate value for the WKBGeometry Point g as a double.
- * Ex:
- * sqlite> SELECT Y(GeomFromBinary(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
- * sqlite> 1.2837445657488
- *
- * @param WKBGeometry structure g containing Point
- *
- * @return double y coordinate
- */
-
-
-/**
- * SQL function Z(g)
- * Returns the Z-coordinate value for the WKBGeometry Point g as a double.
- * Ex:
- * sqlite> SELECT Z(GeomFromBinary(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
- * sqlite> 0.0
- *
- * @param WKBGeometry structure g containing Point
- *
- * @return double z coordinate
- */
-
-
-/**
- * SQL function M(g)
- * Returns the M-value for the WKBGeometry Point g as a double.
- * Ex:
- * sqlite> SELECT M(GeomFromBinary(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
- * sqlite> 5.0
- *
- * @param WKBGeometry structure g containing Point
- *
- * @return double m value
- */
-
-
-
-/**
- * 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> POINT(1.1 1.2 1.3 4.4)
- *
- * @param WKBGeometry structure g containing LineString
- *
- * @return WKBGeometry structure containing WKBPoint
- */
-
-
-/**
- * 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> POINT(5.1 5.2 5.3 4.4)
- *
- * @param WKBGeometry structure g containing LineString
- *
- * @return WKBGeometry structure containing WKBPoint
- */
-
-
-/**
- * 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> 5.8926358638273
- *
- * @param WKBGeometry structure g containing LineString
- *
- * @return double length
- */
-
-
-/**
- * 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> 12
- *
- * @param WKBGeometry structure g containing LineString
- *
- * @return integer number of points
- */
-
-
-/**
- * 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> POINT(1.1 1.2 0.0 0.0)
- *
- * @param WKBGeometry structure g containing LineString
- * @param integer point position starting at 0
- *
- * @return WKBGeometry structure containing WKBPoint
- */
-
-
-/**
- * SQL function IsRing(g)
- * 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> 1
- *
- * @param WKBGeometry structure g containing LineString
- *
- * @return boolean 1 if LineString is a ring 0 if it is not, -1 if NULL
- */
-
-
-
-/*
- * sqlite functions linking
- */
-int sqlite3_extension_init(
- sqlite3 *db,
- char **pzErrMsg,
- 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);
- //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);
- //sqlite3_create_function(db, "M", 1, SQLITE_ANY, 0, M, 0, 0);
- return 0;
-}
-
Added: trunk/sqlfunc.c
===================================================================
--- trunk/sqlfunc.c (rev 0)
+++ trunk/sqlfunc.c 2007-08-23 21:25:37 UTC (rev 3)
@@ -0,0 +1,242 @@
+/**
+ * SQL function toWKT(b)
+ * Returns a WKT openGis Geometry representation from a sqlite blob b.
+ * Ex:
+ * sqlite> SELECT (toWKT(BLOB_POINT)) FROM testGeom WHERE INDEX=1;
+ * sqlite> POINT(1.1 1.2 1.3 4.4)
+ *
+ * @param blob well-known binary to sqlite by its own function
+ *
+ * @return string geometry as well-known text
+ */
+static void toWKT(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ unsigned char *pBlob;
+ int error;
+ char *pString;
+ WKBGeometry Geom;
+
+ //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:
+ //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);
+ //convert to string
+ pString = AsText(&Geom);
+ break;
+ default:
+ sqlite3_result_error(context, messages[4], -1);
+ }
+
+ //write out results
+ sqlite3_result_text(context, pString, -1, SQLITE_TRANSIENT);
+
+ //free AsText allocated memory
+ free(pString);
+}
+
+/**
+ * SQL function toWKB(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)'));
+ *
+ * @param string geometry as well-known text
+ *
+ * @return blob well-known binary to sqlite by its own function
+ */
+static void toWKB(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ int error, sizeBin;
+ char *pBuf;
+ unsigned char *pBin;
+ WKBGeometry Geom;
+
+ //check for null values
+ if(sqlite3_value_type(argv[0]) == SQLITE_NULL) return;
+ //assign parameter
+ pBuf = (char*)sqlite3_value_text(argv[0]);
+
+ error = 0;
+ error = FromText(pBuf, &Geom);
+ if(error) sqlite3_result_error(context, messages[error], -1);
+ //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);
+ sqlite3_result_blob(context, pBin, sizeBin, SQLITE_TRANSIENT);
+
+ //free memory allocated by asBinary()
+ sqlite3_free((void*)pBin);
+}
+
+
+/**
+ * SQL function X(b)
+ * Returns the X-coordinate value for the BLOB b as a double.
+ * Ex:
+ * sqlite> SELECT X(BLOB_POINT) FROM testGeom WHERE INDEX=1;
+ * sqlite> 5.8926358638273
+ *
+ * @param BLOB containing a WKBGeometry
+ *
+ * @return double x coordinate
+ */
+
+
+/**
+ * SQL function Y(b)
+ * Returns the Y-coordinate value for the BLOB b as a double.
+ * Ex:
+ * sqlite> SELECT Y(BLOB_POINT) FROM testGeom WHERE INDEX=1;
+ * sqlite> 1.2837445657488
+ *
+ * @param BLOB containing a WKBGeometry
+ *
+ * @return double y coordinate
+ */
+
+
+/**
+ * SQL function Z(b)
+ * Returns the Z-coordinate value for the BLOB b as a double.
+ * Ex:
+ * sqlite> SELECT Z(BLOB_POINT) FROM testGeom WHERE INDEX=1;
+ * sqlite> 0.0
+ *
+ * @param BLOB containing a WKBGeometry
+ *
+ * @return double z coordinate
+ */
+
+
+/**
+ * SQL function M(b)
+ * Returns the M-value for the BLOB b as a double.
+ * Ex:
+ * sqlite> SELECT M(BLOB_POINT) FROM testGeom WHERE INDEX=1;
+ * sqlite> 5.0
+ *
+ * @param BLOB containing a WKBGeometry
+ *
+ * @return double m value
+ */
+
+
+
+/**
+ * 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> POINT(1.1 1.2 1.3 4.4)
+ *
+ * @param WKBGeometry structure g containing LineString
+ *
+ * @return WKBGeometry structure containing WKBPoint
+ */
+
+
+/**
+ * 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> POINT(5.1 5.2 5.3 4.4)
+ *
+ * @param WKBGeometry structure g containing LineString
+ *
+ * @return WKBGeometry structure containing WKBPoint
+ */
+
+
+/**
+ * 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> 5.8926358638273
+ *
+ * @param WKBGeometry structure g containing LineString
+ *
+ * @return double length
+ */
+
+
+/**
+ * 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> 12
+ *
+ * @param WKBGeometry structure g containing LineString
+ *
+ * @return integer number of points
+ */
+
+
+/**
+ * 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> POINT(1.1 1.2 0.0 0.0)
+ *
+ * @param WKBGeometry structure g containing LineString
+ * @param integer point position starting at 0
+ *
+ * @return WKBGeometry structure containing WKBPoint
+ */
+
+
+/**
+ * SQL function IsRing(g)
+ * 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> 1
+ *
+ * @param WKBGeometry structure g containing LineString
+ *
+ * @return boolean 1 if LineString is a ring 0 if it is not, -1 if NULL
+ */
+
+
+
+/*
+ * sqlite functions linking
+ */
+int sqlite3_extension_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ 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);
+ //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);
+ //sqlite3_create_function(db, "M", 1, SQLITE_ANY, 0, M, 0, 0);
+ return 0;
+}
+
Copied: trunk/wkgfunc.c (from rev 2, trunk/gislite.c)
===================================================================
--- trunk/wkgfunc.c (rev 0)
+++ trunk/wkgfunc.c 2007-08-23 21:25:37 UTC (rev 3)
@@ -0,0 +1,238 @@
+#include <stdio.h>
+#include <string.h>
+//#include <stdlib.h>
+#include <sqlite3ext.h>
+#include "wkg.h"
+#include "wkgfunc.h"
+
+SQLITE_EXTENSION_INIT1
+
+char *messages[5] = {
+ "ok"
+ ,"unrecognized text type"
+ ,"parse error"
+ ,"unrecognized geometry type"
+ ,"unable to convert from blob"
+};
+
+//char debug[50];
+
+void swap_char(char *a,char *b){
+ char c;
+ c = *a;
+ *a=*b;
+ *b=c;
+}
+
+void flip_endian_double(char *d){
+ swap_char(d+7, d);
+ swap_char(d+6, d+1);
+ swap_char(d+5, d+2);
+ swap_char(d+4, d+3);
+}
+
+void flip_endian_int32(char *i){
+ swap_char(i+3, i);
+ swap_char(i+2, i+1);
+}
+/**
+ * function FromText
+ * @param pointer to string in WKT encoding
+ * @param pointer to WKGeometry to be filled
+ *
+ * @return integer error code
+ */
+int FromText(char *pWKT, WKBGeometry *pGeom){
+ int len, i;
+ char *pType;
+ char *pContent;
+
+ //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"
+ //copy geometry type
+ pType = (char*)sqlite3_malloc((int)i+1);
+ strncpy(pType, pWKT, i);
+ //copy geometry content
+ pContent = (char*)sqlite3_malloc((int)len-i);
+ strncpy(pContent, pWKT+i, len-(i-1));
+
+ //assigning structure to blob
+ //POINT(1.1 1.2 1.3 1.4)
+ if(strncmp(pType, "POINT", 5)==0){
+ //fill geometry as point
+ pGeom->point.byteOrder = (byte)'1';//little endian ... autoconf endianness
+ pGeom->point.wkbType = (uint32)3001;
+ if(sscanf(pContent+1, "%lf %lf %lf %lf",
+ &pGeom->point.point.x,
+ &pGeom->point.point.y,
+ &pGeom->point.point.z,
+ &pGeom->point.point.m)
+ !=4){
+ //free all allocated memory
+ sqlite3_free((void*)pType);
+ sqlite3_free((void*)pContent);
+ return 2;//"parse error"
+ }
+ }
+
+ //LINESTRING(1 1 1 1, 2 2 2 2, 3 3 3 3)
+ else if(strncmp(pWKT,"LINESTRING", 10)==0){
+ }
+
+ //POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
+ //MULTIPOINT(0 0, 20 20, 60 60)
+ //MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
+ //MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
+ //GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
+
+ //ERROR
+ else{
+ //free all allocated memory
+ sqlite3_free((void*)pType);
+ sqlite3_free((void*)pContent);
+ return 3;// "unrecognized geometry type"
+ }
+
+ //free all allocated memory
+ sqlite3_free((void*)pType);
+ sqlite3_free((void*)pContent);
+ return 0;
+}
+
+
+/**
+ * function AsText
+ * @param pointer to WKGeometry to be encoded
+ *
+ * @return pointer to string to be filled
+ */
+char* AsText(WKBGeometry *pGeom){
+ int len, i;
+ char *pWKT;
+ //char *sBuffer;
+
+ //create string by wkbType
+ switch(pGeom->point.wkbType){
+ case 3001: // wkbPoint
+ //POINT(1.200000 1.300000 1.400000 1.500000)
+ len = asprintf(&pWKT, "POINT(%lf %lf %lf %lf)"
+ , pGeom->point.point.x
+ , pGeom->point.point.y
+ , pGeom->point.point.z
+ , pGeom->point.point.m);
+ break;
+
+ case 3002: // wkbLineString
+ //LINESTRING(0 0, 10 10, 20 25, 50 60)
+ break;
+
+ //POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
+ //MULTIPOINT(0 0, 20 20, 60 60)
+ //MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
+ //MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
+ //GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
+
+ default:
+ asprintf(&pWKT, "Error: %s", messages[4]);
+ }
+
+ return pWKT;
+}
+
+
+/**
+ * function FromBinary
+ * @param pointer to unsigned char to be read
+ * @param pointer to WKGeometry to be filled
+ *
+ * @return integer error code
+ */
+int FromBinary(unsigned char *pBin, WKBGeometry *pGeom){
+ int bBigendian;
+ uint32 wkbType;
+ int size, sizedbl=sizeof(double);
+
+ //determining endianness from first byte
+ if (pBin[0] == '0') bBigendian = 1; // Big endian
+ else bBigendian = 0; // Little endian
+
+ //type
+ memcpy(&wkbType, pBin+1, sizeof(uint32));
+ //if(bBigendian) flip_endian_int32((char*)&wkbType);
+
+ //set Geometry by wkbType
+ switch(wkbType){
+ case 3001: // wkbPoint
+ pGeom->point.byteOrder = pBin[0];
+ pGeom->point.wkbType = wkbType;
+ //read coordinates from blob into WKBGeometry
+ size = sizeof(byte) + sizeof(uint32);
+ memcpy(&pGeom->point.point.x, pBin+size, sizedbl);
+ size += sizedbl;
+ memcpy(&pGeom->point.point.y, pBin+size, sizedbl);
+ size += sizedbl;
+ memcpy(&pGeom->point.point.z, pBin+size, sizedbl);
+ size += sizedbl;
+ memcpy(&pGeom->point.point.m, pBin+size, sizedbl);
+ break;
+
+ case 3002: // wkbLineString
+ break;
+
+ default:
+ return 4;// "unable to convert from blob"
+ }
+
+ return 0;
+}
+
+
+/**
+ * function AsBinary
+ * @param pointer to WKGeometry to be converted
+ *
+ * @return pointer to unsigned char to be filled
+ */
+unsigned char* AsBinary(WKBGeometry* pGeom){
+ int dim, i;
+ unsigned char *pBuffer;
+ unsigned char *pChar;
+
+ //alloc by wkbType
+ switch(pGeom->point.wkbType){
+ case 3001: // wkbPoint
+ dim = sizeof(WKBPoint);
+ pBuffer = (unsigned char*)sqlite3_malloc(dim);
+ memset(pBuffer, 0, dim);
+ pChar = pBuffer;
+ //fill Geometry
+ memcpy(pChar, (unsigned char*)&pGeom->point.byteOrder, sizeof(byte));
+ pChar += sizeof(byte);
+ memcpy(pChar, &pGeom->point.wkbType, sizeof(uint32));
+ pChar += sizeof(uint32);
+ memcpy(pChar, &pGeom->point.point.x, sizeof(double));
+ pChar += sizeof(double);
+ memcpy(pChar, &pGeom->point.point.y, sizeof(double));
+ pChar += sizeof(double);
+ memcpy(pChar, &pGeom->point.point.z, sizeof(double));
+ pChar += sizeof(double);
+ memcpy(pChar, &pGeom->point.point.m, sizeof(double));
+ break;
+
+ case 3002: // wkbLineString
+ //sprintf(debug,"2");
+ break;
+
+ default:
+ pBuffer = NULL;// "unrecognized geometry type"
+ //to be tested outside
+ }
+
+ return pBuffer;
+}
+
+
Added: trunk/wkgfunc.h
===================================================================
--- trunk/wkgfunc.h (rev 0)
+++ trunk/wkgfunc.h 2007-08-23 21:25:37 UTC (rev 3)
@@ -0,0 +1,16 @@
+#include "wkg.h"
+
+void swap_char(char *a,char *b);
+
+void flip_endian_double(char *d);
+
+void flip_endian_int32(char *i);
+
+int FromText(char *pWKT, WKBGeometry *pGeom);
+
+char* AsText(WKBGeometry *pGeom);
+
+int FromBinary(unsigned char *pBin, WKBGeometry *pGeom);
+
+unsigned char* AsBinary(WKBGeometry* pGeom);
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|