How to access a geometry (point) field in PostGIS database from Django?

JJD

In my project I am using PostgreSQL/PostGIS as the database and Django with django.contrib.gis configured. The existing table pois contains geospatial point data. Here is a excerpt from the SQL create statement:

CREATE TABLE pois
(
  ogc_fid serial NOT NULL,
  the_geom geometry(Point,900914),
  name character varying(254),
  -- ...

I generated the Django model with the following command:

$ python manage.py inspectdb

The generated model looks like this:

from django.contrib.gis.db import models

class POIs(models.Model):
    ogc_fid = models.AutoField(primary_key=True)
    the_geom = models.TextField(blank=True, null=True) # This field type is a guess.
    name = models.CharField(max_length=254, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'pois'

I add the following field to overwrite the default manage with a GeoDjango specific instance:

objects = models.GeoManager()

Now, I want to replace the_geom = models.TextField() with the correct data type which should be either models.PointField() or models.GeometryField(). I tried both. Then I test the model at the Python shell:

$ python manage.py shell
Python 3.4.3 (default, Jul 28 2015, 18:20:59) 
In [1]: from berlin import models
In [2]: models.POIs.objects.first()

This fails and the following stacktrace is output:

/home/user/.virtualenvs/myproject/lib/python3.4/site-packages/django/contrib/ \
    gis/db/models/fields.py in select_format(self, compiler, sql, params)
     57         else:
     58             sel_fmt = '%s'
---> 59         if connection.ops.select:
     60             # This allows operations to be done on fields in the SELECT,
     61             # overriding their values -- used by the Oracle and MySQL

AttributeError: 'DatabaseOperations' object has no attribute 'select'

There is no error when I leave the model with models.TextField. Then the string value is output.

JJD

The database configuration in settings.py was incorrect:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'geodjango',
        'USER': 'geo',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Correct:

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis', # Here
        'NAME': 'geodjango',
        'USER': 'geo',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

I have overseen this in the documentation somehow. Thanks to apollo13 from the #django irc channel for the advice into the right direction.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store geometry Point in Postgis database using java

How to create a table with geometry polygons and field_ID from another table containing field_ID and lat long point in postgis

How can I find point geometry typed tables from postgis?

How to make a generic query to get a point from a Postgis geometry type

How to update a particular point of a complex geometry in Postgis

How to insert a PostGIS GEOMETRY Point in Sequelize ORM?

Map a PostGIS geometry point field with Hibernate on Spring Boot

PostgreSQL, Postgis geometry field

Creating polygon geometry from text field the same table in PostGiS

Insert point geometry into PostGIS using Flask Python

Postgres/Postgis - How to insert geometry in postges (postgis)

How to inherit from boost::geometry::model::point?

SELECT FROM query with PostGIS geometry not working

Create polygon geometry from BBOX coordinates in PostGIS

PostGIS Create Geography Column From Geometry Column

Importing a PostGIS geometry type into Python as a geometry type from Shapely?

PostGIS query to Select all Polygons from Polygon table having an intersecting geometry with one or more point in passed list of GeoCoordinates

How to calculate area around a geometry using postgis?

How to use subquery as a geometry parameter on postgis?

Postgis: How do I select every second point from LINESTRING?

How to get coordinates of a bounding box at a certain distance from a point in postGIS?

How to get closest point from a polygon that is ACTUALLY inside the polygon in Postgis

Django. How to remove a field from objects in the database?

How to send the data from @property field to database tables in Django

How do I enter an "empty" POINT() geometry value into a MySQL field of type POINT?

how to take List<Point> from mil.nga.sf.Geometry

Django Parler how to access translated model field from a mixin

How to access specific value from model choice field in django

Access related database field Django template

TOP Ranking

HotTag

Archive