Changeset 776

Show
Ignore:
Timestamp:
04/12/07 14:08:05 (2 years ago)
Author:
sgillies
Message:

Improve init to handle cases of new and reloaded indexes, and head off file access trouble. Unfortunately, loading indexes does not seem to be working.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Rtree/trunk/rtree/_rtreemodule.cc

    r774 r776  
    4242Rtree_init(Rtree *self, PyObject *args, PyObject *kwds) 
    4343{ 
    44     char* filename = NULL; 
     44    char* basename = NULL; 
     45    char filename[256]; 
    4546    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        ) 
    4961        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))) 
    5679        { 
    5780            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                ); 
    5984            return -1; 
    6085        } 
    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); 
    65101     
    66102    return 0; 
  • Rtree/trunk/rtree/gispyspatialindex.cc

    r774 r776  
    2525 
    2626using 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 } 
    3727 
    3828void GISPySpatialIndex::Initialize() 
     
    6454} 
    6555 
     56// Load a persisted index 
     57GISPySpatialIndex::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 
     68GISPySpatialIndex::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 
    6678GISPySpatialIndex:: ~GISPySpatialIndex() 
    6779{ 
  • Rtree/trunk/rtree/gispyspatialindex.h

    r774 r776  
    4848public: 
    4949  GISPySpatialIndex(); 
     50  GISPySpatialIndex(const char* pszFilename); 
    5051  GISPySpatialIndex(const char* pszFilename, unsigned long nPageSize); 
    5152  ~GISPySpatialIndex(); 
  • Rtree/trunk/rtree/wrapper.cc

    r774 r776  
    5757extern "C" 
    5858GISPySpatialIndex * 
    59 RtreeIndex_new(char* filename, unsigned long nPageLength
     59RtreeIndex_new(char* filename, unsigned long nPageLength, int load
    6060{ 
    6161    if (!filename) 
    6262        return new GISPySpatialIndex; 
    6363    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        } 
    6774    } 
    6875} 
  • Rtree/trunk/rtree/wrapper.h

    r774 r776  
    2727typedef struct RtreeIndex_t *RtreeIndex; 
    2828 
    29 RtreeIndex RtreeIndex_new(char* filename, unsigned long nPagesize); 
     29RtreeIndex RtreeIndex_new(char* filename, unsigned long nPagesize, int load); 
    3030void RtreeIndex_del(RtreeIndex index); 
    3131void RtreeIndex_insertData(RtreeIndex index, long id,  
  • Rtree/trunk/tests/R-Tree.txt

    r774 r776  
    2222------------------ 
    2323 
    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 
    2635 
    27     # Default page size 0 
     36    # Default page size 1 
    2837    >>> index = Rtree("default") 
    2938    >>> for i, coords in enumerate(boxes15): 
     
    3443 
    3544    # Page size 3 
    36     >>> index = Rtree("pagesize3", 3) 
     45    >>> index = Rtree("pagesize3", pagesize=3) 
    3746    >>> for i, coords in enumerate(boxes15): 
    3847    ...     index.add(i, coords) 
     
    4655    ... 
    4756    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