Bonnie Varghese
Bonnie Varghese

Reputation: 2248

Trouble converting POSTGIS query to GeoDjango query

I am new to POSTGIS and GeoDjango. I have a query working in POSTGIS, however, I am having difficulty converting it into a GeoDjango query.

My table world_node contains two columns: node - integer, and mpoint - Geometric Point

POSTGIS SQL Query(Working):

SELECT * FROM world_node 
WHERE ST_CONTAINS(ST_GeomFromText('POLYGON((-100 0, 0 150, 150 0, 0 -100, -100 0))', 4326), mpoint);

I am trying to get all the points that are contained within the polygon specified and it works fine.

Kindly help me with the GeoDjango syntax. Here is my code so far:

poly = GEOSGeometry('Polygon((-100 0, 0 150, 150 0, 0 -100, -100 0))', 4326)
nodes = Nodes.objects.filter(poly__contains=mpoint)

I get the following error:

NameError: name 'mpoint' is not defined

Note: mpoint is a column in my Table and also a field in my Node class in models.py

Any help will be appreciated!

Upvotes: 1

Views: 112

Answers (1)

jcs
jcs

Reputation: 436

Your syntax is not correct, you should switch 'poly' and 'mpoint' like this:

nodes = Nodes.objects.filter(mpoint__contains=poly)

Upvotes: 2

Related Questions