- Timestamp:
- 04/12/07 14:08:05 (2 years ago)
- Files:
-
- Rtree/trunk/rtree/_rtreemodule.cc (modified) (1 diff)
- Rtree/trunk/rtree/gispyspatialindex.cc (modified) (2 diffs)
- Rtree/trunk/rtree/gispyspatialindex.h (modified) (1 diff)
- Rtree/trunk/rtree/wrapper.cc (modified) (1 diff)
- Rtree/trunk/rtree/wrapper.h (modified) (1 diff)
- Rtree/trunk/tests/R-Tree.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
Rtree/trunk/rtree/_rtreemodule.cc
r774 r776 42 42 Rtree_init(Rtree *self, PyObject *args, PyObject *kwds) 43 43 { 44 char* filename = NULL; 44 char* basename = NULL; 45 char filename[256]; 45 46 unsigned long nPageLength = 0; 46 FILE *file = NULL; 47 48 if (!PyArg_ParseTuple(args, "|si", &filename, (unsigned long)&nPageLength)) 47 int overwrite = 0; 48 int load = -1; 49 PyObject *os_module; 50 PyObject *path_module; 51 PyObject *abspath, *dirname; 52 PyObject *func; 53 54 static char *kwlist[] = {"basename", "pagesize", "overwrite", NULL}; 55 56 if (!PyArg_ParseTupleAndKeywords( 57 args, kwds, "|sii", kwlist, 58 &basename, (unsigned long)&nPageLength, &overwrite 59 ) 60 ) 49 61 return -1; 50 51 // Check that there is a file beyond the name 52 if (filename) 53 { 54 file = fopen(filename, "wb"); 55 if (!file) 62 63 // Import os and os.path 64 os_module = PyImport_ImportModule("os"); 65 path_module = PyImport_ImportModule("os.path"); 66 67 if (basename) 68 { 69 snprintf(filename, 256, "%s.dat", basename); 70 71 // Bail out if we don't have write access 72 func = PyObject_GetAttrString(path_module, "abspath"); 73 abspath = PyObject_CallFunction(func, "s", filename); 74 func = PyObject_GetAttrString(path_module, "dirname"); 75 dirname = PyObject_CallFunctionObjArgs(func, abspath, NULL); 76 77 func = PyObject_GetAttrString(os_module, "access"); 78 if (!PyObject_IsTrue(PyObject_CallFunctionObjArgs(func, dirname, PyObject_GetAttrString(os_module, "W_OK"), NULL))) 56 79 { 57 80 PyErr_Format(PyExc_IOError, 58 "Unable to open file '%s' for index storage", filename); 81 "Unable to open file '%s' for index storage", 82 basename 83 ); 59 84 return -1; 60 85 } 61 fclose(file); 62 } 63 64 self->index = RtreeIndex_new(filename, nPageLength); 86 87 // Check for existence with os.path.exists 88 func = PyObject_GetAttrString(path_module, "exists"); 89 if (PyObject_IsTrue(PyObject_CallFunction(func, "s", filename))) 90 { 91 if (overwrite == 0) 92 load = 1; 93 else 94 load = 0; 95 } 96 else 97 load = 0; 98 } 99 100 self->index = RtreeIndex_new(basename, nPageLength, load); 65 101 66 102 return 0; Rtree/trunk/rtree/gispyspatialindex.cc
r774 r776 25 25 26 26 using namespace SpatialIndex; 27 28 GISPySpatialIndex::GISPySpatialIndex(const char* pszFilename, unsigned long nPageSize)29 {30 31 std::string oFilename = std::string(pszFilename);32 mStorageManager = StorageManager::createNewDiskStorageManager(oFilename, nPageSize);33 34 Initialize();35 36 }37 27 38 28 void GISPySpatialIndex::Initialize() … … 64 54 } 65 55 56 // Load a persisted index 57 GISPySpatialIndex::GISPySpatialIndex(const char* filename) 58 { 59 60 std::string oFilename = std::string(filename); 61 mStorageManager = StorageManager::loadDiskStorageManager(oFilename); 62 63 Initialize(); 64 65 } 66 67 // Create a new index 68 GISPySpatialIndex::GISPySpatialIndex(const char* filename, unsigned long pagesize) 69 { 70 71 std::string oFilename = std::string(filename); 72 mStorageManager = StorageManager::createNewDiskStorageManager(oFilename, pagesize); 73 74 Initialize(); 75 76 } 77 66 78 GISPySpatialIndex:: ~GISPySpatialIndex() 67 79 { Rtree/trunk/rtree/gispyspatialindex.h
r774 r776 48 48 public: 49 49 GISPySpatialIndex(); 50 GISPySpatialIndex(const char* pszFilename); 50 51 GISPySpatialIndex(const char* pszFilename, unsigned long nPageSize); 51 52 ~GISPySpatialIndex(); Rtree/trunk/rtree/wrapper.cc
r774 r776 57 57 extern "C" 58 58 GISPySpatialIndex * 59 RtreeIndex_new(char* filename, unsigned long nPageLength )59 RtreeIndex_new(char* filename, unsigned long nPageLength, int load) 60 60 { 61 61 if (!filename) 62 62 return new GISPySpatialIndex; 63 63 else 64 { 65 if (!nPageLength) nPageLength=1; 66 return new GISPySpatialIndex(filename, nPageLength); 64 { 65 if (load == 1) 66 { 67 return new GISPySpatialIndex(filename); 68 } 69 else 70 { 71 if (!nPageLength) nPageLength=1; 72 return new GISPySpatialIndex(filename, nPageLength); 73 } 67 74 } 68 75 } Rtree/trunk/rtree/wrapper.h
r774 r776 27 27 typedef struct RtreeIndex_t *RtreeIndex; 28 28 29 RtreeIndex RtreeIndex_new(char* filename, unsigned long nPagesize );29 RtreeIndex RtreeIndex_new(char* filename, unsigned long nPagesize, int load); 30 30 void RtreeIndex_del(RtreeIndex index); 31 31 void RtreeIndex_insertData(RtreeIndex index, long id, Rtree/trunk/tests/R-Tree.txt
r774 r776 22 22 ------------------ 23 23 24 Existing index data will be clobbered! This is a known issue of the underlying 25 C++ lib. Patches gratefully accepted. 24 # Clean up leftover data 25 >>> import os 26 >>> try: 27 ... os.remove('default.dat') 28 ... os.remove('default.idx') 29 ... os.remove('pagesize3.dat') 30 ... os.remove('pagesize3.idx') 31 ... os.remove('testing.dat') 32 ... os.remove('testing.idx') 33 ... except OSError: 34 ... pass 26 35 27 # Default page size 036 # Default page size 1 28 37 >>> index = Rtree("default") 29 38 >>> for i, coords in enumerate(boxes15): … … 34 43 35 44 # Page size 3 36 >>> index = Rtree("pagesize3", 3)45 >>> index = Rtree("pagesize3", pagesize=3) 37 46 >>> for i, coords in enumerate(boxes15): 38 47 ... index.add(i, coords) … … 46 55 ... 47 56 IOError: Unable to open file 'bogus/foo' for index storage 57 58 # Load a persisted index 59 >>> _ = os.system("cp default.dat testing.dat") 60 >>> _ = os.system("cp default.idx testing.idx") 61 62 >>> index = Rtree("testing") 63 >>> hits = [n for n in index.intersection((0, 0, 60, 60))] 64 >>> len(hits) 65 10 66
