WIP: Use GeoJSON and Shapely instead of EWKB and django.contrib.gis.geos
Closes #827 (closed)
I am having a bad time trying to do CPU profiling as most flamegraph-related tools are either obsolete (older versions of Django only, or Python 2), or unreliable (skipping many function calls), so I can't exactly tell the current ratio of time spent dealing with polygons.
From tests iterating over 45 million elements in the shell, I can see that using ST_AsGeoJSON
with a JSONField to parse the GeoJSON and retrieve the coordinates list, then Shapely to check the polygon, is twice as fast as using the normal EWKB method. Using ST_AsGeoJSON(polygon)::json->'coordinates'
to get PostgreSQL to retrieve the coordinates by itself is only 30% faster instead of 50%, so parsing in Python and doing json.loads(value)['coordinates']
is faster.
I cannot see any meaningful changes in ListElements
or ListElementChildren
when querying the default 20 elements. I can see a slight change with the maximum of 500 elements per page, but the CPU profiling does not confirm that less time is spent on polygons specifically. I can also see a slight enhancement with ListProcessElements
.
I do not think the benefits outweigh the costs here, because the hacks necessary to get this custom field to work are very flaky and it is no longer possible to use .filter(polygon=...)
or .get_or_create(polygon=...)
.