547 |
*/ |
*/ |
548 |
static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds) |
static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds) |
549 |
{ |
{ |
550 |
char* file; |
char* file = NULL; |
551 |
char* mode = "rb"; |
char* mode = "rb"; |
552 |
static char *kwlist[] = {"name", "mode", NULL}; |
static char *kwlist[] = {"name", "mode", NULL}; |
553 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:__init__", kwlist, |
|
554 |
Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
SHPClose(self->handle); |
555 |
|
self->handle = NULL; |
556 |
self->handle = SHPOpen(file, mode); |
|
557 |
|
#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES) |
558 |
|
if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ |
559 |
|
PyObject *wfile; |
560 |
|
if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:ShapeFile", kwlist, &wfile, &mode)) |
561 |
|
{ |
562 |
|
PyObject *wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL); |
563 |
|
if (!wmode) return -1; |
564 |
|
self->handle = SHPOpenW(PyUnicode_AS_UNICODE(wfile), PyUnicode_AS_UNICODE(wmode)); |
565 |
|
Py_DECREF(wmode); |
566 |
|
if (!self->handle) |
567 |
|
{ |
568 |
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile); |
569 |
|
return -1; |
570 |
|
} |
571 |
|
} |
572 |
|
else |
573 |
|
{ |
574 |
|
/* Drop the argument parsing error as narrow |
575 |
|
strings are also valid. */ |
576 |
|
PyErr_Clear(); |
577 |
|
} |
578 |
|
} |
579 |
|
#endif |
580 |
|
|
581 |
if (!self->handle) |
if (!self->handle) |
582 |
{ |
{ |
583 |
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:ShapeFile", kwlist, |
584 |
|
Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
585 |
|
self->handle = SHPOpen(file, mode); |
586 |
|
|
587 |
|
if (!self->handle) |
588 |
|
{ |
589 |
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
590 |
|
PyMem_Free(file); |
591 |
|
return -1; |
592 |
|
} |
593 |
|
|
594 |
|
PyMem_Free(file); |
595 |
} |
} |
596 |
|
|
597 |
PyMem_Free(file); |
return 0; |
|
return self->handle ? 0 : -1; |
|
598 |
} |
} |
599 |
|
|
600 |
|
|
601 |
|
|
602 |
static PyObject* shapefile_close(ShapeFileObject* self) |
static PyObject* shapefile_close(ShapeFileObject* self) |
603 |
{ |
{ |
604 |
SHPClose(self->handle); |
SHPClose(self->handle); |
606 |
Py_RETURN_NONE; |
Py_RETURN_NONE; |
607 |
} |
} |
608 |
|
|
609 |
|
|
610 |
|
|
611 |
static PyObject* shapefile_info(ShapeFileObject* self) |
static PyObject* shapefile_info(ShapeFileObject* self) |
612 |
{ |
{ |
613 |
SHPHandle handle = self->handle; |
SHPHandle handle = self->handle; |
617 |
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
618 |
} |
} |
619 |
|
|
620 |
|
|
621 |
|
|
622 |
static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args) |
static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args) |
623 |
{ |
{ |
624 |
int index; |
int index; |
644 |
return (PyObject*) result; |
return (PyObject*) result; |
645 |
} |
} |
646 |
|
|
647 |
|
|
648 |
|
|
649 |
static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args) |
static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args) |
650 |
{ |
{ |
651 |
int index, result; |
int index, result; |
722 |
char* file; |
char* file; |
723 |
int type; |
int type; |
724 |
ShapeFileObject* result; |
ShapeFileObject* result; |
725 |
|
SHPHandle handle = NULL; |
726 |
if (!PyArg_ParseTuple(args, "eti:create", Py_FileSystemDefaultEncoding, &file, &type)) return NULL; |
int wideargument = 0; |
727 |
|
|
728 |
|
#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES) |
729 |
|
if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ |
730 |
|
PyObject *wfile; |
731 |
|
if (PyArg_ParseTuple(args, "Ui:create", &wfile, &type)) |
732 |
|
{ |
733 |
|
wideargument = 1; |
734 |
|
handle = SHPCreateW(PyUnicode_AS_UNICODE(wfile), type); |
735 |
|
if (!handle) |
736 |
|
{ |
737 |
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile); |
738 |
|
return NULL; |
739 |
|
} |
740 |
|
} |
741 |
|
else |
742 |
|
{ |
743 |
|
/* Drop the argument parsing error as narrow |
744 |
|
strings are also valid. */ |
745 |
|
PyErr_Clear(); |
746 |
|
} |
747 |
|
} |
748 |
|
#endif |
749 |
|
|
750 |
|
if (!handle) |
751 |
|
{ |
752 |
|
if (!PyArg_ParseTuple(args, "eti:create", Py_FileSystemDefaultEncoding, &file, &type)) return NULL; |
753 |
|
handle = SHPCreate(file, type); |
754 |
|
if (!handle) |
755 |
|
{ |
756 |
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
757 |
|
PyMem_Free(file); |
758 |
|
return NULL; |
759 |
|
} |
760 |
|
PyMem_Free(file); |
761 |
|
} |
762 |
|
|
763 |
result = PyObject_New(ShapeFileObject, &ShapeFileType); |
result = PyObject_New(ShapeFileObject, &ShapeFileType); |
764 |
if (!result) |
if (!result) |
765 |
{ |
{ |
766 |
PyMem_Free(file); |
SHPClose(handle); |
767 |
return PyErr_NoMemory(); |
return PyErr_NoMemory(); |
768 |
} |
} |
769 |
|
|
770 |
result->handle = SHPCreate(file, type); |
result->handle = handle; |
|
PyMem_Free(file); |
|
|
|
|
|
if (!result->handle) |
|
|
{ |
|
|
PyObject_Del((PyObject*)result); |
|
|
PyErr_SetString(PyExc_RuntimeError, "Failed to create ShapeFile"); |
|
|
return NULL; |
|
|
} |
|
|
|
|
771 |
return (PyObject*) result; |
return (PyObject*) result; |
772 |
} |
} |
773 |
|
|