/[thuban]/branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/libraries/pyshapelib/shapelibmodule.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2744 by bramz, Thu Mar 15 13:48:58 2007 UTC revision 2745 by bramz, Thu Mar 15 22:27:02 2007 UTC
# Line 67  static void shpobject_dealloc(SHPObjectO Line 67  static void shpobject_dealloc(SHPObjectO
67          self->ob_type->tp_free((PyObject*)self);          self->ob_type->tp_free((PyObject*)self);
68  }  }
69    
70    static int unpack_vertex(PyObject* vertex, int vertex_type,
71                    double* xs, double* ys, double* zs, double* ms, int offset);
72    
73  /* The constructor of SHPObject. parts is a list of lists of tuples  /* The constructor of SHPObject. parts is a list of lists of tuples
74  * describing the parts and their vertices just likethe output of the  * describing the parts and their vertices just likethe output of the
75  * vertices() method. part_type_list is the list of part-types and may  * vertices() method. part_type_list is the list of part-types and may
# Line 97  static int shpobject_init(SHPObjectObjec Line 100  static int shpobject_init(SHPObjectObjec
100          int has_m;          int has_m;
101                    
102          int i;          int i;
103          int ok, return_code = -1;          int return_code = -1;
104                    
105          /* first, unpack parameters */          /* first, unpack parameters */
106          if (kwds != NULL && PyDict_Size(kwds) > 0)          if (kwds != NULL && PyDict_Size(kwds) > 0)
# Line 202  static int shpobject_init(SHPObjectObjec Line 205  static int shpobject_init(SHPObjectObjec
205                  for (j = 0; j < length; ++j)                  for (j = 0; j < length; ++j)
206                  {                  {
207                          PyObject* vertex = PySequence_ITEM(part, j);                          PyObject* vertex = PySequence_ITEM(part, j);
208                          switch (vertex_type)                          if (!unpack_vertex(vertex, vertex_type, xs, ys, zs, ms, part_start + j))
                         {  
                         case vtXY:  
                                 ok = PyArg_ParseTuple(vertex, "dd:__init__", xs + part_start + j, ys + part_start + j);  
                                 break;  
                         case vtXYM:  
                                 ok = PyArg_ParseTuple(vertex, "ddd:__init__", xs + part_start + j, ys + part_start + j, ms + part_start + j);  
                                 break;  
                         case vtXYZM:  
                                 ms[part_start + j] = 0.;  
                                 ok = PyArg_ParseTuple(vertex, "ddd|d:__init__", xs + part_start + j, ys + part_start + j, zs + part_start + j,  
                                         ms + part_start + j);  
                                 break;  
                         }  
                         Py_DECREF(vertex);  
                         if (!ok)  
209                          {                          {
210                                    Py_DECREF(vertex);
211                                  PyErr_SetString(PyExc_TypeError, "at least one vertex is of the wrong format");                                  PyErr_SetString(PyExc_TypeError, "at least one vertex is of the wrong format");
212                                  goto exit;                                  goto exit;
213                          }                          }
214                            Py_DECREF(vertex);
215                  }                  }
216                  Py_DECREF(part);                  Py_DECREF(part);
217                  part = NULL;                  part = NULL;
# Line 242  exit: Line 232  exit:
232          return return_code;          return return_code;
233  }  }
234    
235    /* helper for shpobject_init. Unpacks vertices
236     */
237    static int unpack_vertex(PyObject* vertex, int vertex_type,
238                    double* xs, double* ys, double* zs, double* ms, int offset)
239    {
240            int ok;
241            PyObject* m_object;
242            PyObject *err_type, *err_value, *err_traceback;
243    
244            switch (vertex_type)
245            {
246            case vtXY:
247                    return PyArg_ParseTuple(vertex, "dd:__init__", xs + offset, ys + offset);
248    
249            case vtXYM:
250                    ms[offset] = PYSHAPELIB_NO_DATA;
251                    ok = PyArg_ParseTuple(vertex, "dd|d:__init__", xs + offset, ys + offset, ms + offset);
252                    if (!ok)
253                    {
254                            /* maybe they specified None as M value */
255                            PyErr_Fetch(&err_type, &err_value, &err_traceback);
256                            ok = PyArg_ParseTuple(vertex, "ddO:__init__", xs + offset, ys + offset, &m_object);
257                            if (ok && m_object == Py_None)
258                            {
259                                    Py_XDECREF(err_type);
260                                    Py_XDECREF(err_value);
261                                    Py_XDECREF(err_traceback);
262                            }
263                            else
264                            {
265                                    PyErr_Restore(err_type, err_value, err_traceback);
266                            }
267                    }
268                    return ok;
269    
270            case vtXYZM:
271                    zs[offset] = 0.;
272                    ms[offset] = PYSHAPELIB_NO_DATA;
273                    ok = PyArg_ParseTuple(vertex, "dd|dd:__init__", xs + offset, ys + offset,
274                                    zs + offset, ms + offset);
275                    if (!ok)
276                    {
277                            /* maybe they specified None as M value */
278                            PyErr_Fetch(&err_type, &err_value, &err_traceback);
279                            ok = PyArg_ParseTuple(vertex, "dddO:__init__", xs + offset, ys + offset,
280                                            zs + offset, &m_object);
281                            if (ok && m_object == Py_None)
282                            {
283                                    Py_XDECREF(err_type);
284                                    Py_XDECREF(err_value);
285                                    Py_XDECREF(err_traceback);
286                            }
287                            else
288                            {
289                                    PyErr_Restore(err_type, err_value, err_traceback);
290                            }
291                    }
292                    return ok;
293    
294            default:
295                    PyErr_SetString(PyExc_NotImplementedError, "vertex type not implemented");
296                    return 0;
297            }
298    }
299    
300  /*  /*
301  * The extents() method of SHPObject.  * The extents() method of SHPObject.
302  *  *
# Line 340  static PyObject* build_vertex_list(SHPOb Line 395  static PyObject* build_vertex_list(SHPOb
395                  case vtXYM:                  case vtXYM:
396                          vertex = Py_BuildValue("ddd", object->padfX[index], object->padfY[index],                          vertex = Py_BuildValue("ddd", object->padfX[index], object->padfY[index],
397                                  object->padfM[index]);                                  object->padfM[index]);
398                            break;
399                  case vtXYZM:                  case vtXYZM:
400                          vertex = Py_BuildValue("dddd", object->padfX[index], object->padfY[index],                          vertex = Py_BuildValue("dddd", object->padfX[index], object->padfY[index],
401                                  object->padfZ[index], object->padfM[index]);                                  object->padfZ[index], object->padfM[index]);
# Line 437  static PyObject* shpobject_repr(SHPObjec Line 493  static PyObject* shpobject_repr(SHPObjec
493  static struct PyMethodDef shpobject_methods[] =  static struct PyMethodDef shpobject_methods[] =
494  {  {
495          {"extents", (PyCFunction)shpobject_extents, METH_NOARGS,          {"extents", (PyCFunction)shpobject_extents, METH_NOARGS,
496                  "extents()\n"                  "extents() -> ((x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max))\n\n"
497                  "returns ((x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max)), the 4D bounding box of the SHPObject"},                  "returns the 4D bounding box of the SHPObject"},
498          {"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS,          {"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS,
499                  "vertices()\n"                  "vertices() ->  [[(x, y, ...), ...], ...]\n\n"
500                  "returns [[(x, y, ...), ...], ...], a list of object parts, where each part is again a list of vertices.\n"                  "Returns a list of object parts, where each part is again a list of vertices. "
501                  "each vertex is a tuple of two to four doubles, depending on the object type."},                  "Each vertex is a tuple of two to four doubles, depending on the object type."},
502          {"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS,          {"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS,
503                  "part_types()\n"                  "part_types() -> tuple\n\n"
504                  "returns a tuple of integers, each integer indicating the type of the corresponding part in vertices()"},                  "returns a tuple of integers, each integer indicating the type of the "
505                    "corresponding part in vertices()"},
506          {NULL}          {NULL}
507  };  };
508    
# Line 492  static int shapefile_init(ShapeFileObjec Line 549  static int shapefile_init(ShapeFileObjec
549  {  {
550          char* file;          char* file;
551          char* mode = "rb";          char* mode = "rb";
552          if (kwds != NULL && PyDict_Size(kwds) > 0)          static char *kwlist[] = {"name", "mode", NULL};
553          {          if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:__init__", kwlist,
554                  PyErr_Format(PyExc_TypeError, "shapelib.ShapeFile.__init__ takes no keyword arguments");                  Py_FileSystemDefaultEncoding, &file, &mode)) return -1;
                 return -1;  
         }  
         if (!PyArg_ParseTuple(args, "et|s:__init__", Py_FileSystemDefaultEncoding, &file, &mode)) return -1;  
555                    
556          self->handle = SHPOpen(file, mode);          self->handle = SHPOpen(file, mode);
557            PyMem_Free(file);
558            
559          return self->handle ? 0 : -1;          return self->handle ? 0 : -1;
560  }  }
561    
# Line 580  static PyObject* shapefile_repr(ShapeFil Line 636  static PyObject* shapefile_repr(ShapeFil
636  static struct PyMethodDef shapefile_methods[] =  static struct PyMethodDef shapefile_methods[] =
637  {  {
638          {"close", (PyCFunction)shapefile_close, METH_NOARGS,          {"close", (PyCFunction)shapefile_close, METH_NOARGS,
639                  "close()\n"                  "close() -> None\n\n"
640                  "close the shape file" },                  "close the shape file" },
641          {"info", (PyCFunction)shapefile_info, METH_NOARGS,          {"info", (PyCFunction)shapefile_info, METH_NOARGS,
642                  "info()\n"                  "info() -> (num_shapes, type, (x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max))\n\n"
643                  "returns (num_shapes, type, (x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max)) with:\n"                  "returns info about ShapeFile with:\n"
644                  "-num_shapes: the number of the objects in the file\n"                  "- num_shapes: the number of the objects in the file\n"
645                  "-type: the type of the shape file (SHPT_POINT, SHPT_POLYGON, ...)\n"                  "- type: the type of the shape file (SHPT_POINT, SHPT_POLYGON, ...)\n"
646                  "-(x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max): 4D bounding box of the data in the shape file" },                  "- (x_min, ...), (x_max, ...): 4D bounding box of the data in the shape file" },
647          {"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS,          {"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS,
648                  "read_object(id)\n"                  "read_object(id) -> SHPObject\n\n"
649                  "Return object indexed by id" },                  "Returns shape indexed by id" },
650          {"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS,          {"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS,
651                  "write_object(id, object)\n"                  "write_object(id, object) -> id\n\n"
652                  "Write an object at index id.\n"                  "Write an object at index id, and returns id."
653                  "If id == -1, the object is appended at the end of the shape file"},                  "If id == -1, the object is appended at the end of the shape file."},
654          {"cobject", (PyCFunction)shapefile_cobject, METH_NOARGS,          {"cobject", (PyCFunction)shapefile_cobject, METH_NOARGS,
655                  "cobject()\n"                  "cobject() -> CObject\n\n"
656                  "Return the shapelib SHPHandle as a Python CObject"},                  "Return the shapelib SHPHandle as a Python CObject"},
657          {NULL}          {NULL}
658  };  };
# Line 626  static PyObject* shapelib_create(PyObjec Line 682  static PyObject* shapelib_create(PyObjec
682          result = PyObject_New(ShapeFileObject, &ShapeFileType);          result = PyObject_New(ShapeFileObject, &ShapeFileType);
683          if (!result)          if (!result)
684          {          {
685                    PyMem_Free(file);
686                  return PyErr_NoMemory();                  return PyErr_NoMemory();
687          }          }
688                    
689          result->handle = SHPCreate(file, type);          result->handle = SHPCreate(file, type);
690            PyMem_Free(file);
691            
692          if (!result->handle)          if (!result->handle)
693          {          {
694                  PyObject_Del((PyObject*)result);                  PyObject_Del((PyObject*)result);
# Line 671  static PyObject* shapelib_part_type_name Line 730  static PyObject* shapelib_part_type_name
730  static struct PyMethodDef shapelib_methods[] =  static struct PyMethodDef shapelib_methods[] =
731  {  {
732          {"open", (PyCFunction)shapelib_open, METH_VARARGS,          {"open", (PyCFunction)shapelib_open, METH_VARARGS,
733                  "open(filename [, mode='rb'])\n"                  "open(name [, mode='rb']) -> ShapeFile\n\n"
734                  "open a ShapeFile" },                  "opens a ShapeFile" },
735          {"create", (PyCFunction)shapelib_create, METH_VARARGS,          {"create", (PyCFunction)shapelib_create, METH_VARARGS,
736                  "create(filename, type)\n"                  "create(name, type) -> ShapeFile\n\n"
737                  "create a ShapeFile of a certain type (one of SHPT_POINT, SHPT_POLYGON)" },                  "creates a ShapeFile of a certain type (one of SHPT_POINT, SHPT_POLYGON)" },
738          {"c_api", (PyCFunction)shapelib_c_api, METH_NOARGS,          {"c_api", (PyCFunction)shapelib_c_api, METH_NOARGS,
739                  "c_api()\n"                  "c_api() -> CObject\n\n"
740                  "get C API of shapelib" },                  "get C API of shapelib as a CObject" },
741          {"type_name", (PyCFunction)shapelib_type_name, METH_VARARGS,          {"type_name", (PyCFunction)shapelib_type_name, METH_VARARGS,
742                  "type_name(type)\n"                  "type_name(type) -> string\n\n"
743                  "return type as string" },                  "return type as string" },
744          {"part_type_name", (PyCFunction)shapelib_part_type_name, METH_VARARGS,          {"part_type_name", (PyCFunction)shapelib_part_type_name, METH_VARARGS,
745                  "part_type_name(part_type)\n"                  "part_type_name(part_type) -> string\n\n"
746                  "return part type as string" },                  "return part type as string" },
747          {NULL}          {NULL}
748  };  };

Legend:
Removed from v.2744  
changed lines
  Added in v.2745

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26