Import Polygon Data from geojson files into PostGIS using GeoDjango

852 views
Skip to first unread message

Ankit Singh

unread,
Sep 21, 2016, 12:22:15 PM9/21/16
to geodjango
Am using GeoDjango with PostGIS as Backend. I have few geojson files with features as following example. GeoDjango has provided information to import shape files and other formats but geojson formats. How do I import these geojson files into DB?

 "features": [
{ "type": "Feature", "properties": { "SubName": "X", "SubSubName": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 70.693828, 37.074496 ], [ 70.693828, 37.074496 ] ] ] } }
,
{ "type": "Feature", "properties": { "SubName": "X", "SubSubName": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 70.502913, 27.065997 ],  [ 70.502913, 27.065997 ] ] ] } }


Am using GeoDjango with PostGIS as Backend. I have few geojson files with features as following example. GeoDjango has provided information to import shape files and other formats but geojson formats. How do I import these geojson files into DB?

  "features": [
{ "type": "Feature", "properties": { "SubName": "X", "SubSubName": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 70.693828, 37.074496 ], [ 70.693828, 37.074496 ] ] ] } }
,
{ "type": "Feature", "properties": { "SubName": "X", "SubSubName": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 70.502913, 27.065997 ],  [ 70.502913, 27.065997 ] ] ] } }

I have two core requirements:

  1. Load the Subregion polygons on webportal using D3JS Map library
  2. When i have a lat-long, find in which polygon does this point exist and get the SubName & SubSubName from the polygon properties.

I have posted this on Stack Overflow http://stackoverflow.com/questions/39617055/import-polygon-data-from-geojson-files-into-postgis-using-geodjango


Thanks in Advance.

Claude Paroz

unread,
Sep 21, 2016, 12:28:45 PM9/21/16
to geod...@googlegroups.com
Le 21. 09. 16 à 16:34, Ankit Singh a écrit :
> Am using GeoDjango with PostGIS as Backend. I have few geojson files
> with features as following example. GeoDjango has provided information
> to import shape files and other formats but geojson formats. How do I
> import these geojson files into DB?
>
> |"features":[{"type":"Feature","properties":{"SubName":"X","SubSubName":"A"},"geometry":{"type":"Polygon","coordinates":[[[70.693828,37.074496],[70.693828,37.074496]]]}},{"type":"Feature","properties":{"SubName":"X","SubSubName":"B"},"geometry":{"type":"Polygon","coordinates":[[[70.502913,27.065997],[70.502913,27.065997]]]}}|

Hi Ankit,

You have an example of creating a geometry with GeoJSON input here:
https://docs.djangoproject.com/en/1.10/ref/contrib/gis/geos/#creating-a-geometry

So you'll have to read the GeoJSON structure, extract the "geometry"
content of each feature, create the geometry as presented in the docs,
then you can do what you want with it (save in the DB, or anything else).

Regards,

Claude
--
www.2xlibre.net

Ankit Singh

unread,
Sep 21, 2016, 1:07:22 PM9/21/16
to geodjango
Thanks Calude for your warm response.

I have used MongoDB previously for storing GeoJSON's alone. I have a fair idea on reading GeoJSON and extracting polygon coordinates, but no idea on how to save it in PostGIS. Does GeoDjango not handling the DB saving stuff? 

Do i have to create a model for saving coordinates data? Does the coordinates array need to be Text field?

Regards.

Claude Paroz

unread,
Sep 21, 2016, 1:37:31 PM9/21/16
to geod...@googlegroups.com
Le 21. 09. 16 à 19:07, Ankit Singh a écrit :
> Thanks Calude for your warm response.
>
> I have used MongoDB previously for storing GeoJSON's alone. I have a
> fair idea on reading GeoJSON and extracting polygon coordinates, but no
> idea on how to save it in PostGIS. Does GeoDjango not handling the DB
> saving stuff?
>
> Do i have to create a model for saving coordinates data? Does the
> coordinates array need to be Text field?

Yes, the proper way to do it is to have a model with at least one
geometry field, then saving the model instance will save the content in
PostGIS. To sum up:

- Read the "geometry" part of the GeoJSON structure
- Create a django.contrib.gis.geos.Polygon object with the above as input.
- Create or get an instance of a Django model containing a geometry
field and set the above Polygon as the geometry field value.
- Save the model

Another more direct way would be to call some raw SQL and wrapping the
geometry inside PostGISAdapter. But you are on your own in this
scenario, as this is undocumented.

https://docs.djangoproject.com/en/1.10/topics/db/sql/#executing-custom-sql-directly

Claude

>
> On Wednesday, September 21, 2016 at 9:58:45 PM UTC+5:30, Claude wrote:
>
> Le 21. 09. 16 à 16:34, Ankit Singh a écrit :
> > Am using GeoDjango with PostGIS as Backend. I have few geojson files
> > with features as following example. GeoDjango has provided
> information
> > to import shape files and other formats but geojson formats. How do I
> > import these geojson files into DB?
> >
> >
> |"features":[{"type":"Feature","properties":{"SubName":"X","SubSubName":"A"},"geometry":{"type":"Polygon","coordinates":[[[70.693828,37.074496],[70.693828,37.074496]]]}},{"type":"Feature","properties":{"SubName":"X","SubSubName":"B"},"geometry":{"type":"Polygon","coordinates":[[[70.502913,27.065997],[70.502913,27.065997]]]}}|
>
>
> Hi Ankit,
>
> You have an example of creating a geometry with GeoJSON input here:
> https://docs.djangoproject.com/en/1.10/ref/contrib/gis/geos/#creating-a-geometry
> <https://docs.djangoproject.com/en/1.10/ref/contrib/gis/geos/#creating-a-geometry>
>
>
> So you'll have to read the GeoJSON structure, extract the "geometry"
> content of each feature, create the geometry as presented in the docs,
> then you can do what you want with it (save in the DB, or anything
> else).
>
> Regards,
>
> Claude
> --
> www.2xlibre.net <http://www.2xlibre.net>
Reply all
Reply to author
Forward
0 new messages