Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#9877 closed (fixed)

More Pythonic mutations of geometry objects

Reported by: Aryeh Leib Taurog <vim@…> Owned by: jbronn
Component: GIS Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Experimenting with geodjango, I found myself immediately wanting easier,
more Pythonic ways to manipulate the data in the GEOS geometry objects:

geometryObj[i:j] = ((33.42,-70.89),(34.819,-67.61))

So I have extended the LineString and LinearRing classes' __getitem__ and
__setitem__ methods to allow slicing, added a __delitem__ while I was at it, and threw in most
of the non-special, standard list methods as well.

I'd like to do this for the geometry collections next, but I'm posting
this here (is there a better place?) to solicit feedback on these mods
before I continue.


  1. Is there some really good reason not to do this that I'm missing, or am

I just the first one who wanted it enough to sit down and do it?

  1. Did I do this correctly? It seems to work. I tested the correctness of

the mutations themselves, but haven't extensively tested the GEOS
methods on the mutated objects.

  1. I've created a situation where
del geometryObj[:]  # now works
geometryObj = LinearString([]) # doesn't work

Is there some reason I shouldn't do that?

  1. Does it make sense to implement similar mutation methods for Point

objects or Polygon objects?

  1. I think it would be nice to make the LinearRing mutations fool-proof.

Meaning since the first and last point must be the same, a LinearRing
object with n points should really behave like a list of n - 1
coordinates, all the while maintaining the last coordinate as a mirror of
the first so that the geometry won't be invalidated by the mutations.

  1. I implemented count() which is redundant with __len__() and, in this

case, num_points(). Overkill?

  1. Also I'm not sure if index(x) and remove(x) made sense in this

context, but I could imagine cases where they might be useful and so I
erred on the side of implementing as much of the standard list interface as
I thought reasonable. Comments?

  1. The sort() and reverse() methods don't seem applicable here. These are the

only standard list methods which I did not implement. However, I think
that they might apply to the geometry collections. Comments?

Attachments (3)

pythonic_mutation_patch_part_I (17.8 KB) - added by Aryeh Leib Taurog <vim@…> 7 years ago.
This is a patch to the django 1.0.2 release
pythonic_mutation_patch_part_I.2 (19.3 KB) - added by Aryeh Leib Taurog <vim@…> 7 years ago.
This is an updated patch to Django 1.0.2 release
pythonic_mutation_patch_part_II (28.5 KB) - added by Aryeh Leib Taurog <vim@…> 7 years ago.
More complete list-style mutations for Geometry Collections; refactored code

Download all attachments as: .zip

Change History (10)

Changed 7 years ago by Aryeh Leib Taurog <vim@…>

This is a patch to the django 1.0.2 release

Changed 7 years ago by Aryeh Leib Taurog <vim@…>

This is an updated patch to Django 1.0.2 release

comment:1 Changed 7 years ago by Aryeh Leib Taurog <vim@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Regarding question 2 above, I've attached an updated patch with additional tests checking that the following GEOS properties work as expected on the mutated geometries:


comment:2 Changed 7 years ago by Aryeh Leib Taurog <vim@…>

I've completed a second round of modifications and am attaching a patch which supersedes the previous patches.

I modified the GeometryCollection class with list-like, slice-enabled mutations. I then refactored the code, placing the bulk of the modifications in a ListMixin class, which I added as a superclass to both LineString and GeometryCollection. I'm sure a similar mixin exists already somewhere (the UserList module did not seem to be appropriate) but I didn't know where to look.

I also modified the tests (with a little bending backwards) to exercise both the geometries and the geometry collections. Currently the tests operate on LineString, LinearRing, MultiPoint, and MultiLineString.

I am still relatively new both to Django and to Python, so I'd love some feedback on style, etc.

Changed 7 years ago by Aryeh Leib Taurog <vim@…>

More complete list-style mutations for Geometry Collections; refactored code

comment:3 Changed 7 years ago by Aryeh Leib Taurog <vim@…>

Note that the pythonic_mutation_patch_part_II attachment is also a patch to the django 1.0.2 release and supersedes the patches I attached earlier.

comment:4 Changed 7 years ago by jbronn

  • milestone set to 1.1

comment:5 Changed 7 years ago by jbronn

  • Owner changed from nobody to jbronn
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 7 years ago by jbronn

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [10131]) Refactored the GEOS interface. Improvements include:

  • Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
  • Added support for GEOS prepared geometries via prepared property. Prepared geometries significantly speed up certain operations.
  • Added support for GEOS cascaded union as MultiPolygon.cascaded_union property.
  • Added support for GEOS line merge as merged property on LineString, and MultiLineString geometries. Thanks, Paul Smith.
  • No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as WKTReader, WKTWriter, WKBReader, and WKBWriter (which supports 3D and SRID inclusion)
  • Moved each type of geometry to their own module, eliminating the cluttered
  • Internally, all C API methods are explicitly called from a module rather than a star import.

Fixed #9557, #9877, #10222

comment:3 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top