{"id":582,"date":"2019-05-17T18:29:52","date_gmt":"2019-05-17T12:59:52","guid":{"rendered":"http:\/\/blog.nuventure.in\/?p=582"},"modified":"2023-04-03T18:56:49","modified_gmt":"2023-04-03T13:26:49","slug":"geodjango","status":"publish","type":"post","link":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/","title":{"rendered":"GeoDjango"},"content":{"rendered":"\n<p>GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a toolbox of utilities for building GIS web applications and also bindings to popular spatial libraries such as GEOS, GDAL, and GeoIP, which can be used separately without Django in any Python application or interactively in the shell.<\/p>\n\n\n\n<p>Let&#8217;s get started by installing Python 3.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python3 --version<br><\/pre>\n\n\n\n<p>Now let&#8217;s install GeoDjango dependencies by running the following commands.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo aptitude install gdal-bin libgdal-dev <br>$ sudo aptitude install python3-gdal <br><\/pre>\n\n\n\n<p>Now, we have to create spatial databases. PostGIS 2 ships with PostgreSQL as an extension for spatial functionality. Geodjango also supports Sqlite (via Spatialite), MySql and Oracle backends.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ createdb  &lt;db name&gt; <br>$ psql &lt;db name&gt; <br>&gt; CREATE EXTENSION postgis;<strong><br><\/strong><\/pre>\n\n\n\n<p>Let&#8217;s now get started with our project. Let&#8217;s call our project <em>geodjango<\/em> by running:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ django-admin startproject geodjango<\/pre>\n\n\n\n<p>This will initialize the project. Now, we can create a django project within our project.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd geodjango <br>$ python manage.py startapp world<\/pre>\n\n\n\n<p>Let&#8217;s edit the database connection in <em>geodjango\/settings.py<\/em> to match our setup.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DATABASES = {<br>     'default': {<br>          'ENGINE': 'django.contrib.gis.db.backends.postgis',<br>          'NAME': 'geodjango',<br>          'USER': 'geo',<br>     },<br> }<\/pre>\n\n\n\n<p>In addition, modify the <a href=\"https:\/\/django.readthedocs.io\/en\/2.1.x\/ref\/settings.html#std:setting-INSTALLED_APPS\"><code>INSTALLED_APPS<\/code><\/a> setting to include <a href=\"https:\/\/django.readthedocs.io\/en\/2.1.x\/ref\/contrib\/admin\/index.html#module-django.contrib.admin\"><code>django.contrib.admin<\/code><\/a>, <a href=\"https:\/\/django.readthedocs.io\/en\/2.1.x\/ref\/contrib\/gis\/index.html#module-django.contrib.gis\"><code>django.contrib.gis<\/code><\/a>, and <code>world<\/code> (your newly created application):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSTALLED_APPS = [<br>     'django.contrib.admin',<br>     'django.contrib.auth',<br>     'django.contrib.contenttypes',<br>     'django.contrib.sessions',<br>     'django.contrib.messages',<br>     'django.contrib.staticfiles',<br>     'django.contrib.gis',<br>     'world',<br> ]<\/pre>\n\n\n\n<p class=\"has-large-font-size\">Geographic Data<\/p>\n\n\n\n<p class=\"has-medium-font-size\">World Borders<\/p>\n\n\n\n<p>We can avail the data of the world borders from <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/thematicmapping.org\/downloads\/TM_WORLD_BORDERS-0.3.zip\" target=\"_blank\">Here.<\/a> Let&#8217;s create a data directory in the world application, download the world borders data, and unzip. Use the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir world\/data\n$ cd world\/data\n$ wget https:\/\/thematicmapping.org\/downloads\/TM_WORLD_BORDERS-0.3.zip\n$ unzip TM_WORLD_BORDERS-0.3.zip\n$ cd ..\/..<\/code><\/pre>\n\n\n\n<p>The world borders ZIP file contains a set of data files which is one of the most popular geospatial data formats known as an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Shapefile\">ESRI Shapefile<\/a>.  When unzipped, the world borders dataset includes files with the following extensions:<\/p>\n\n\n\n<ul><li><code><code>.shp<\/code><\/code>: Holds the vector data for the world borders geometries.<\/li><li><code><code>.shx<\/code><\/code>: Spatial index file for geometries stored in the <code><code>.shp<\/code><\/code>.<\/li><li><code><code>.dbf<\/code><\/code>: Database file for holding non-geometric attribute data (e.g., integer and character fields).<\/li><li><code><code>.prj<\/code><\/code>: Contains the spatial reference information for the geographic data stored in the shapefile.<\/li><\/ul>\n\n\n\n<p>The GDAL <code><code>ogrinfo<\/code><\/code> utility allows examining the metadata of shapefiles or other vector data sources:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ogrinfo world\/data\/TM_WORLD_BORDERS-0.3.shp <br>INFO: Open of `world\/data\/TM_WORLD_BORDERS-0.3.shp'<br>       using driver `ESRI Shapefile' successful.<br>1: TM_WORLD_BORDERS-0.3 (Polygon)<br><\/pre>\n\n\n\n<p><code><code>ogrinfo<\/code><\/code> tells us that the shapefile has one layer and that this layer contains polygon data.  Let&#8217;s specify the layer name and use the <code><code>-so<\/code><\/code> option to get the important summary information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ogrinfo -so world\/data\/TM_WORLD_BORDERS-0.3.shp TM_WORLD_BORDERS-0.3\nINFO: Open of `world\/data\/TM_WORLD_BORDERS-0.3.shp'\n      using driver `ESRI Shapefile' successful.\n\nLayer name: TM_WORLD_BORDERS-0.3\nGeometry: Polygon\nFeature Count: 246\nExtent: (-180.000000, -90.000000) - (180.000000, 83.623596)\nLayer SRS WKT:\nGEOGCS[\"GCS_WGS_1984\",\n    DATUM[\"WGS_1984\",\n        SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],\n    PRIMEM[\"Greenwich\",0.0],\n    UNIT[\"Degree\",0.0174532925199433]]\nFIPS: String (2.0)\nISO2: String (2.0)\nISO3: String (3.0)\nUN: Integer (3.0)\nNAME: String (50.0)\nAREA: Integer (7.0)\nPOP2005: Integer (10.0)\nREGION: Integer (3.0)\nSUBREGION: Integer (3.0)\nLON: Real (8.3)\nLAT: Real (7.3)<\/code><\/pre>\n\n\n\n<p>This detailed summary information tells us the number of features in the layer, the geographic bounds of the data, the spatial reference system (\u201cSRS WKT\u201d), as well as type information for each attribute field. For example, <code><code>FIPS: String (2.0)<\/code><\/code> indicates that the <code><code>FIPS<\/code><\/code> character field has a maximum length of 2.  Similarly, <code><code>LON: Real (8.3)<\/code><\/code> is a floating-point field that holds a maximum of 8 digits, up to three decimal places.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Geographic Models<\/h2>\n\n\n\n<h5 class=\"wp-block-heading\">Defining a Geographic Model<\/h5>\n\n\n\n<p>Let&#8217;s  create a GeoDjango model to represent the data:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.contrib.gis.db import models\n\nclass WorldBorder(models.Model):\n    # Regular Django fields corresponding to the attributes in the\n    # world borders shapefile.\n    name = models.CharField(max_length=50)\n    area = models.IntegerField()\n    pop2005 = models.IntegerField('Population 2005')\n    fips = models.CharField('FIPS Code', max_length=2)\n    iso2 = models.CharField('2 Digit ISO', max_length=2)\n    iso3 = models.CharField('3 Digit ISO', max_length=3)\n    un = models.IntegerField('United Nations Code')\n    region = models.IntegerField('Region Code')\n    subregion = models.IntegerField('Sub-Region Code')\n    lon = models.FloatField()\n    lat = models.FloatField()\n\n    # GeoDjango-specific: a geometry field (MultiPolygonField)\n    mpoly = models.MultiPolygonField()\n\n    # Returns the string representation of the model.\n    def __str__(self):\n        return self.name<\/code><\/pre>\n\n\n\n<p>The default spatial reference system for geometry fields is WGS84 . In other words, the field coordinates are in longitude, latitude pairs in units of degrees.  To use a different coordinate system, let&#8217;s set the SRID of the geometry field with the <code><code>srid<\/code><\/code> argument. Use an integer representing the coordinate system\u2019s EPSG code.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Run <code>migrate<\/code><\/h5>\n\n\n\n<p>After defining your model, we need to sync it with the database. First, we need to create a database migration. Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py makemigrations\nMigrations for 'world':\n  world\/migrations\/0001_initial.py:\n    - Create model WorldBorder<\/code><\/pre>\n\n\n\n<p>You may inspect the raw code generated by the above migration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py sqlmigrate world 0001<\/code><\/pre>\n\n\n\n<p>The output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN;\n--\n-- Create model WorldBorder\n--\nCREATE TABLE \"world_worldborder\" (\n    \"id\" serial NOT NULL PRIMARY KEY,\n    \"name\" varchar(50) NOT NULL,\n    \"area\" integer NOT NULL,\n    \"pop2005\" integer NOT NULL,\n    \"fips\" varchar(2) NOT NULL,\n    \"iso2\" varchar(2) NOT NULL,\n    \"iso3\" varchar(3) NOT NULL,\n    \"un\" integer NOT NULL,\n    \"region\" integer NOT NULL,\n    \"subregion\" integer NOT NULL,\n    \"lon\" double precision NOT NULL,\n    \"lat\" double precision NOT NULL\n    \"mpoly\" geometry(MULTIPOLYGON,4326) NOT NULL\n)\n;\nCREATE INDEX \"world_worldborder_mpoly_id\" ON \"world_worldborder\" USING GIST ( \"mpoly\" );\nCOMMIT;<\/code><\/pre>\n\n\n\n<p>Now, let&#8217;s run <code><code>migrate<\/code><\/code> to execute the sql:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py migrate\nOperations to perform:\n  Apply all migrations: admin, auth, contenttypes, sessions, world\nRunning migrations:\n  ...\n  Applying world.0001_initial... OK\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">GDAL Interface<\/h3>\n\n\n\n<p>GeoDjango also includes a Pythonic interface to GDAL\u2019s powerful OGR\nlibrary that can work with all the vector data sources that OGR supports.<\/p>\n\n\n\n<p>First, let&#8217;s invoke the Django shell:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py shell<\/code><\/pre>\n\n\n\n<p>We can determine the path of <code>World Border<\/code> data using Python\u2019s built-in <code>os<\/code> module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> import os\n>>> import world\n>>> world_shp = os.path.abspath(os.path.join(os.path.dirname(world.__file__),\n...                             'data', 'TM_WORLD_BORDERS-0.3.shp'))\n<\/code><\/pre>\n\n\n\n<p>Now, open the world borders shapefile using GeoDjango\u2019s <code><code>DataSource<\/code><\/code> interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> from django.contrib.gis.gdal import DataSource\n>>> ds = DataSource(world_shp)\n>>> print(ds)\n\/ ... \/geodjango\/world\/data\/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)\n<\/code><\/pre>\n\n\n\n<p>Data source objects can have different layers of geospatial features; however, shapefiles are only allowed to have one layer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> print(len(ds))\n1\n>>> lyr = ds[0]\n>>> print(lyr)\nTM_WORLD_BORDERS-0.3<\/code><\/pre>\n\n\n\n<p>We can see the layer\u2019s geometry type the features it contains:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> print(lyr.geom_type)\nPolygon\n>>> print(len(lyr))\n246<\/code><\/pre>\n\n\n\n<p>The <code><code>Layer<\/code><\/code> may also have a spatial reference system associated with it.  If it does, the <code><code>srs<\/code><\/code> attribute will return a <code><code>SpatialReference<\/code><\/code> object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> srs = lyr.srs\n>>> print(srs)\nGEOGCS[\"GCS_WGS_1984\",\n    DATUM[\"WGS_1984\",\n        SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],\n    PRIMEM[\"Greenwich\",0.0],\n    UNIT[\"Degree\",0.0174532925199433]]\n>>> srs.proj4 # PROJ.4 representation\n'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs '<\/code><\/pre>\n\n\n\n<p>This shapefile is in the popular WGS84 spatial reference system.<\/p>\n\n\n\n<p>In addition, shapefiles also support attribute fields that may contain\nadditional data.  Here are the fields on the World Borders layer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> print(lyr.fields)\n['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']<\/code><\/pre>\n\n\n\n<p>The following code will let you examine the OGR types (e.g. integer or string) associated with each of the fields:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> [fld.__name__ for fld in lyr.field_types]\n['OFTString', 'OFTString', 'OFTString', 'OFTInteger', 'OFTString', 'OFTInteger', 'OFTInteger', 'OFTInteger', 'OFTInteger', 'OFTReal', 'OFTReal']<\/code><\/pre>\n\n\n\n<p>We can iterate over each feature in the layer and extract information from both the feature\u2019s geometry as well as the feature\u2019s attribute fields (whose <strong>values<\/strong> are accessed via <code><code>get()<\/code><\/code> method):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> for feat in lyr:\n...    print(feat.get('NAME'), feat.geom.num_points)\n...\nGuernsey 18\nJersey 26\nSouth Georgia South Sandwich Islands 338\nTaiwan 363<\/code><\/pre>\n\n\n\n<p><code><code>Layer<\/code><\/code> objects may be sliced:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> lyr[0:2]\n[&lt;django.contrib.gis.gdal.feature.Feature object at 0x2f47690>, &lt;django.contrib.gis.gdal.feature.Feature object at 0x2f47650>]<\/code><\/pre>\n\n\n\n<p>And individual features may be retrieved by their feature ID:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> feat = lyr[234]\n>>> print(feat.get('NAME'))\nSan Marino<\/code><\/pre>\n\n\n\n<p>Boundary geometries may be exported as <code>WKT<\/code> and <code>GeoJSON<\/code>. Both are plaintext representations of shapes which are originally encoded in binary.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> geom = feat.geom\n>>> print(geom.wkt)\nPOLYGON ((12.415798 43.957954,12.450554 ...\n>>> print(geom.json)\n{ \"type\": \"Polygon\", \"coordinates\": [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>LayerMapping<\/code><\/h3>\n\n\n\n<p>To import the data, use a LayerMapping in a Python script. Create a file called <code><code>load.py<\/code><\/code> inside the <code><code>world<\/code><\/code> application, with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nfrom django.contrib.gis.utils import LayerMapping\nfrom .models import WorldBorder\n\nworld_mapping = {\n    'fips' : 'FIPS',\n    'iso2' : 'ISO2',\n    'iso3' : 'ISO3',\n    'un' : 'UN',\n    'name' : 'NAME',\n    'area' : 'AREA',\n    'pop2005' : 'POP2005',\n    'region' : 'REGION',\n    'subregion' : 'SUBREGION',\n    'lon' : 'LON',\n    'lat' : 'LAT',\n    'mpoly' : 'MULTIPOLYGON',\n}\n\nworld_shp = os.path.abspath(\n    os.path.join(os.path.dirname(__file__), 'data', 'TM_WORLD_BORDERS-0.3.shp'),\n)\n\ndef run(verbose=True):\n    lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False)\n    lm.save(strict=True, verbose=verbose)<\/code><\/pre>\n\n\n\n<p>Let&#8217;s  invoke the Django shell from the <code>geodjango<\/code> project directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py shell<\/code><\/pre>\n\n\n\n<p>Next, let&#8217;s import the <code><code>load<\/code><\/code> module, call the <code><code>run<\/code><\/code> routine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> from world import load\n>>> load.run()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">grinspect<br><\/h3>\n\n\n\n<p>The <code><code>ogrinspect<\/code><\/code> command introspects a GDAL-supported vector data source (e.g., a shapefile) and generates a model definition and <code><code>LayerMapping<\/code><\/code> dictionary automatically.<\/p>\n\n\n\n<p>The general usage of the command goes as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py ogrinspect [options] &lt;data_source> &lt;model_name> [options]<\/code><\/pre>\n\n\n\n<p><code><code>data_source<\/code><\/code> is the path to the GDAL-supported data source and <code><code>model_name<\/code><\/code> is the name to use for the model.  Command-line options may be used to further define how the model is generated.<\/p>\n\n\n\n<p>For example, the following command nearly reproduces the <code><code>WorldBorder<\/code><\/code> model and mapping dictionary created above, automatically:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py ogrinspect world\/data\/TM_WORLD_BORDERS-0.3.shp WorldBorder \\\n    --srid=4326 --mapping --multi<\/code><\/pre>\n\n\n\n<p>The command produces the following output, which may be copied directly into the <code><code>models.py<\/code><\/code> of a GeoDjango application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This is an auto-generated Django model module created by ogrinspect.\nfrom django.contrib.gis.db import models\n\nclass WorldBorder(models.Model):\n    fips = models.CharField(max_length=2)\n    iso2 = models.CharField(max_length=2)\n    iso3 = models.CharField(max_length=3)\n    un = models.IntegerField()\n    name = models.CharField(max_length=50)\n    area = models.IntegerField()\n    pop2005 = models.IntegerField()\n    region = models.IntegerField()\n    subregion = models.IntegerField()\n    lon = models.FloatField()\n    lat = models.FloatField()\n    geom = models.MultiPolygonField(srid=4326)\n\n# Auto-generated `LayerMapping` dictionary for WorldBorder model\nworldborders_mapping = {\n    'fips' : 'FIPS',\n    'iso2' : 'ISO2',\n    'iso3' : 'ISO3',\n    'un' : 'UN',\n    'name' : 'NAME',\n    'area' : 'AREA',\n    'pop2005' : 'POP2005',\n    'region' : 'REGION',\n    'subregion' : 'SUBREGION',\n    'lon' : 'LON',\n    'lat' : 'LAT',\n    'geom' : 'MULTIPOLYGON',\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Spatial Queries<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Spatial Lookups<\/h3>\n\n\n\n<p>GeoDjango adds spatial lookups to the Django ORM.  We can find the country in the <code><code>WorldBorder<\/code><\/code> table that a point belongs to.  Let&#8217;s fire up the management shell:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python manage.py shell<\/pre>\n\n\n\n<p>Define the point of interest:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> pnt_wkt = 'POINT(-95.3385 29.7245)'<\/code><\/pre>\n\n\n\n<p>The <code><code>pnt_wkt<\/code><\/code> string represents the point at -95.3385 degrees longitude, 29.7245 degrees latitude.  The geometry is in a format known as Well Known Text (WKT), a standard issued by the Open Geospatial Consortium (OGC). [ Import the <code><code>WorldBorder<\/code><\/code> model, and perform a <code><code>contains<\/code><\/code> lookup using the <code><code>pnt_wkt<\/code><\/code> as the parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> from world.models import WorldBorder\n>>> WorldBorder.objects.filter(mpoly__contains=pnt_wkt)\n&lt;QuerySet [&lt;WorldBorder: United States>]><\/code><\/pre>\n\n\n\n<p>We can also use a <code>GEOS geometry object<\/code>. Here, we can combine the <code><code>intersects<\/code><\/code> spatial lookup with the <code><code>get<\/code><\/code> method to retrieve only the <code><code>WorldBorder<\/code><\/code> instance for San Marino instead of a queryset:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> from django.contrib.gis.geos import Point\n>>> pnt = Point(12.4604, 43.9420)\n>>> WorldBorder.objects.get(mpoly__intersects=pnt)\n&lt;WorldBorder: San Marino><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Automatic Spatial Transformations<\/h3>\n\n\n\n<p>When doing spatial queries, GeoDjango automatically transforms\ngeometries if they\u2019re in a different coordinate system.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> from django.contrib.gis.geos import GEOSGeometry, Point\n>>> pnt = Point(954158.1, 4215137.1, srid=32140)<\/code><\/pre>\n\n\n\n<p><code><code>pnt<\/code><\/code> may also be constructed with EWKT, an \u201cextended\u201d form of WKT that includes the SRID:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> pnt = GEOSGeometry('SRID=32140;POINT(954158.1 4215137.1)')<\/code><\/pre>\n\n\n\n<p>GeoDjango\u2019s ORM will automatically wrap geometry values in transformation SQL, allowing the developer to work at a higher level of abstraction:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> qs = WorldBorder.objects.filter(mpoly__intersects=pnt)\n>>> print(qs.query) # Generating the SQL\nSELECT \"world_worldborder\".\"id\", \"world_worldborder\".\"name\", \"world_worldborder\".\"area\",\n\"world_worldborder\".\"pop2005\", \"world_worldborder\".\"fips\", \"world_worldborder\".\"iso2\",\n\"world_worldborder\".\"iso3\", \"world_worldborder\".\"un\", \"world_worldborder\".\"region\",\n\"world_worldborder\".\"subregion\", \"world_worldborder\".\"lon\", \"world_worldborder\".\"lat\",\n\"world_worldborder\".\"mpoly\" FROM \"world_worldborder\"\nWHERE ST_Intersects(\"world_worldborder\".\"mpoly\", ST_Transform(%s, 4326))\n>>> qs # printing evaluates the queryset\n&lt;QuerySet [&lt;WorldBorder: United States>]><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Lazy Geometries<\/h3>\n\n\n\n<p>GeoDjango loads geometries in a standardized textual representation.  When the geometry field is first accessed, GeoDjango creates a <code><code>GEOSGeometry<\/code><\/code> object, exposing powerful functionality, such as serialization properties for popular geospatial formats:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> sm = WorldBorder.objects.get(name='San Marino')\n>>> sm.mpoly\n&lt;MultiPolygon object at 0x24c6798>\n>>> sm.mpoly.wkt # WKT\nMULTIPOLYGON (((12.4157980000000006 43.9579540000000009, 12.4505540000000003 43.9797209999999978, ...\n>>> sm.mpoly.wkb # WKB (as Python binary buffer)\n&lt;read-only buffer for 0x1fe2c70, size -1, offset 0 at 0x2564c40>\n>>> sm.mpoly.geojson # GeoJSON\n'{ \"type\": \"MultiPolygon\", \"coordinates\": [ [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...<\/code><\/pre>\n\n\n\n<p>This includes access to all of the advanced geometric operations provided by the GEOS library:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> pnt = Point(12.4604, 43.9420)\n>>> sm.mpoly.contains(pnt)\nTrue\n>>> pnt.contains(sm.mpoly)\nFalse<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Putting your data on the map<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Geographic Admin<\/h3>\n\n\n\n<p>GeoDjango extends Django\u2019s admin application with support for editing geometry fields.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Basics<\/h4>\n\n\n\n<p>GeoDjango also supplements the Django admin by allowing users to create and modify geometries on a JavaScript slippy map.<\/p>\n\n\n\n<p>Let\u2019s create a file called <code><code>admin.py<\/code><\/code> inside the <code><code>world<\/code><\/code> application with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.contrib.gis import admin\nfrom .models import WorldBorder\n\nadmin.site.register(WorldBorder, admin.GeoModelAdmin)<\/code><\/pre>\n\n\n\n<p>Next, edit the <code><code>urls.py<\/code><\/code> in the <code><code>geodjango<\/code><\/code> application folder as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.contrib.gis import admin\nfrom django.urls import include, path\n\nurlpatterns = [\n    path('admin\/', admin.site.urls),\n]<\/code><\/pre>\n\n\n\n<p>Create an admin user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py createsuperuser<\/code><\/pre>\n\n\n\n<p>Next, start up the Django development server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py runserver<\/code><\/pre>\n\n\n\n<p>Browse in to <code><code><code>http:\/\/localhost:8000\/admin\/<\/code><\/code><\/code>, and log in with the user you just created. Browse to any of the <code><code>WorldBorder<\/code><\/code> entries. The borders may be edited by clicking on a polygon and dragging the vertices to the desired position.<\/p>\n\n\n\n<p>GeoDjango turns Django into an effective geographic Web framework. GeoDjango eases the effort of making GIS (Geographic information system) web apps with location-based services.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a toolbox of utilities for building GIS web applications and also bindings to popular spatial libraries such as GEOS, GDAL, and GeoIP, which can be used separately without Django in any Python application or interactively [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":795,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[18],"tags":[65,16],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Nuventure Blog GeoDjango -<\/title>\n<meta name=\"description\" content=\"Nuventure Blog GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nuventure Blog GeoDjango -\" \/>\n<meta property=\"og:description\" content=\"Nuventure Blog GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/\" \/>\n<meta property=\"og:site_name\" content=\"Nuventure Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/nuventureco\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-05-17T12:59:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-03T13:26:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"422\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Steve\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@nuventureco\" \/>\n<meta name=\"twitter:site\" content=\"@nuventureco\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Steve\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/\"},\"author\":{\"name\":\"Steve\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#\/schema\/person\/e42795dd493946ecd146143ac76ff1a6\"},\"headline\":\"GeoDjango\",\"datePublished\":\"2019-05-17T12:59:52+00:00\",\"dateModified\":\"2023-04-03T13:26:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/\"},\"wordCount\":1200,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg\",\"keywords\":[\"GeoDjango\",\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/\",\"url\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/\",\"name\":\"Nuventure Blog GeoDjango -\",\"isPartOf\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg\",\"datePublished\":\"2019-05-17T12:59:52+00:00\",\"dateModified\":\"2023-04-03T13:26:49+00:00\",\"description\":\"Nuventure Blog GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a\",\"breadcrumb\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage\",\"url\":\"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg\",\"contentUrl\":\"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg\",\"width\":750,\"height\":422},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/nuventureconnect.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"GeoDjango\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#website\",\"url\":\"https:\/\/nuventureconnect.com\/blog\/\",\"name\":\"Nuventure Blog\",\"description\":\"Knowledge.transmit!\",\"publisher\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/nuventureconnect.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#organization\",\"name\":\"Nuventure Connect Private Limited\",\"url\":\"https:\/\/nuventureconnect.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2023\/03\/logo-main-with-cartion-1.webp\",\"contentUrl\":\"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2023\/03\/logo-main-with-cartion-1.webp\",\"width\":200,\"height\":89,\"caption\":\"Nuventure Connect Private Limited\"},\"image\":{\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/nuventureco\/\",\"https:\/\/x.com\/nuventureco\",\"https:\/\/www.instagram.com\/nuventure\/\",\"https:\/\/in.linkedin.com\/company\/nuventure\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#\/schema\/person\/e42795dd493946ecd146143ac76ff1a6\",\"name\":\"Steve\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/nuventureconnect.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/304e12c24de1b4770ca00ae1dd506c29?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/304e12c24de1b4770ca00ae1dd506c29?s=96&r=g\",\"caption\":\"Steve\"},\"url\":\"https:\/\/nuventureconnect.com\/blog\/author\/steve\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nuventure Blog GeoDjango -","description":"Nuventure Blog GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/","og_locale":"en_US","og_type":"article","og_title":"Nuventure Blog GeoDjango -","og_description":"Nuventure Blog GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a","og_url":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/","og_site_name":"Nuventure Blog","article_publisher":"https:\/\/www.facebook.com\/nuventureco\/","article_published_time":"2019-05-17T12:59:52+00:00","article_modified_time":"2023-04-03T13:26:49+00:00","og_image":[{"width":750,"height":422,"url":"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg","type":"image\/jpeg"}],"author":"Steve","twitter_card":"summary_large_image","twitter_creator":"@nuventureco","twitter_site":"@nuventureco","twitter_misc":{"Written by":"Steve","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#article","isPartOf":{"@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/"},"author":{"name":"Steve","@id":"https:\/\/nuventureconnect.com\/blog\/#\/schema\/person\/e42795dd493946ecd146143ac76ff1a6"},"headline":"GeoDjango","datePublished":"2019-05-17T12:59:52+00:00","dateModified":"2023-04-03T13:26:49+00:00","mainEntityOfPage":{"@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/"},"wordCount":1200,"commentCount":0,"publisher":{"@id":"https:\/\/nuventureconnect.com\/blog\/#organization"},"image":{"@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage"},"thumbnailUrl":"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg","keywords":["GeoDjango","python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/","url":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/","name":"Nuventure Blog GeoDjango -","isPartOf":{"@id":"https:\/\/nuventureconnect.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage"},"image":{"@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage"},"thumbnailUrl":"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg","datePublished":"2019-05-17T12:59:52+00:00","dateModified":"2023-04-03T13:26:49+00:00","description":"Nuventure Blog GeoDjango is an included contrib module to make easy GIS (Geographic Information System) web apps with location-based services. GeoDjango provides a","breadcrumb":{"@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#primaryimage","url":"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg","contentUrl":"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2019\/05\/305714_e8f2_3.jpg","width":750,"height":422},{"@type":"BreadcrumbList","@id":"https:\/\/nuventureconnect.com\/blog\/2019\/05\/17\/geodjango\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nuventureconnect.com\/blog\/"},{"@type":"ListItem","position":2,"name":"GeoDjango"}]},{"@type":"WebSite","@id":"https:\/\/nuventureconnect.com\/blog\/#website","url":"https:\/\/nuventureconnect.com\/blog\/","name":"Nuventure Blog","description":"Knowledge.transmit!","publisher":{"@id":"https:\/\/nuventureconnect.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nuventureconnect.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/nuventureconnect.com\/blog\/#organization","name":"Nuventure Connect Private Limited","url":"https:\/\/nuventureconnect.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nuventureconnect.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2023\/03\/logo-main-with-cartion-1.webp","contentUrl":"https:\/\/nuventureconnect.com\/blog\/wp-content\/uploads\/2023\/03\/logo-main-with-cartion-1.webp","width":200,"height":89,"caption":"Nuventure Connect Private Limited"},"image":{"@id":"https:\/\/nuventureconnect.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/nuventureco\/","https:\/\/x.com\/nuventureco","https:\/\/www.instagram.com\/nuventure\/","https:\/\/in.linkedin.com\/company\/nuventure"]},{"@type":"Person","@id":"https:\/\/nuventureconnect.com\/blog\/#\/schema\/person\/e42795dd493946ecd146143ac76ff1a6","name":"Steve","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nuventureconnect.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/304e12c24de1b4770ca00ae1dd506c29?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/304e12c24de1b4770ca00ae1dd506c29?s=96&r=g","caption":"Steve"},"url":"https:\/\/nuventureconnect.com\/blog\/author\/steve\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/posts\/582"}],"collection":[{"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/comments?post=582"}],"version-history":[{"count":16,"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/posts\/582\/revisions"}],"predecessor-version":[{"id":796,"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/posts\/582\/revisions\/796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/media\/795"}],"wp:attachment":[{"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/media?parent=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/categories?post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nuventureconnect.com\/blog\/wp-json\/wp\/v2\/tags?post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}