Skip to content

Instantly share code, notes, and snippets.

@djq
Last active November 2, 2015 16:06
Show Gist options
  • Save djq/58558ac644a6c15f52ac to your computer and use it in GitHub Desktop.
Save djq/58558ac644a6c15f52ac to your computer and use it in GitHub Desktop.
Intro to spatial data using django
# ----
# Background
# ----
#
# Vectors:
# vectors consist of points, lines, polygons
# Within GeoDjango, data-types which are multipolygon and geometry also exist
#
# Projections:
# Projection systems facilitate translating data between a 3D and 2D space.
# The default for web-maps is EPSG:4326
# ----
# Open a geodjango shell
# ----
# import the Point function
from django.contrib.gis.geos import Point
# create a point
point = Point(5, 23)
# create a point with a specific projection
point = Point(1, 1, srid=4326)
# examine the point in json form
point.json
# import the Polygon function
from django.contrib.gis.geos import Polygon
# create a polyon (first and last coordinates are the same, which closes the polygon)
poly = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)), srid=4326)
# ---
# some simple spatial queries
# ---
# is the point in the polygon?
point.within(poly)
# what is the area of the polygon?
poly.area
# how far away is the point from the centroid of the polygon?
point.distance(poly.centroid)
# make a polygon (circle) by buffering the point
point_circle = point.buffer(10)
# create a new polygon by getting the difference between these polygons
poly_difference = poly.difference(point_circle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment