0

I am working on an application, where I have shapefiles (.shp).
I have read those shapes into my PostgreSQL database and I can show those shape coordinates over Google Map.

Now I'm having a use-case, I need to divide these LGA (Local Governance Area) into sub area (like divide an LGA into 10km2 sub-area).

Does anyone have any idea how can I do this using Python/Django/GeoDjango?

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
Mahendra Garg
  • 516
  • 1
  • 9
  • 27
  • 1
    Your question is pretty general (too broad) and that is not acceptable under StackOverflow (or gis.stackexchange for that matter) standards. Have a look here and try to reformat your question: stackoverflow.com/help/how-to-ask – John Moutafis Nov 24 '17 at 14:06

1 Answers1

1

Since you are using PostgreSQL and PostGIS you can utilize the ST_Split and ST_Dump database functions to make one type of polygon split.

  1. Use the method from another thread: How to make/use a custom database function in Django (Disclaimer: that is a Q&A I wrote) to wrap those database methods for Django use:

    from django.contrib.gis.db.models.functions import GeoFunc
    
    class SplitGeom(GeoFunc):
        function='ST_Split'
    
    class DumpGeom(GeoFunc):
        function='ST_Dump'
    
  2. We will modify the example provided in ST_Split's documentation, assuming that you have a model named MyModel with a polygon field named lga and a distinct pk.

  3. Create a line/multiline (or another Geometry that suits you) to use as "blade" to cut/divide a specific polygon. Let's call that line blade:

    blade = LineString((lon_1, lat_1), (lon_2, lat_2))
    
  4. Use the wrapped methods with annotate to add a field containing the created polygons into a MyModel object:

    MyModel.objects.annotate(
        subareas=DumpGeom(
            SplitGeom('lga', blade)
        ) 
    )
    

Important: This is not THE method, it is a method which can help you grasp the logic of what you can do with GeoDjango.

Another example of how to split polygons into PostgreSQL: http://www.gistutor.com/postgresqlpostgis/6-advanced-postgresqlpostgis-tutorials/21-how-to-cut-or-split-a-polygon-using-a-line-with-postgis.html

John Moutafis
  • 22,254
  • 11
  • 68
  • 112