Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install:...
Transcript of Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install:...
Geospatial database queries in
Drupal 7 using the GeoField Module
Jeremy Chinquist
1
Example of a Geospatial Query
What is the area of this region?
A geospatial attribute
SELECT AREA ( _some_geospatial_column__ ) FROM [table]
What is the miminum bounding box? another attribute
What locations are in this region?
A geospatial relationship of 2 geospatial objects
SELECT entity_id FROM region a INNER JOIN location b ON ST_Contains ( a.region,
b.location) = 1
What is the distance between two locations?
Use ST_Distance(g1,g2)
Geometry Model: Point
3
POINT (x y)
POINT (longitude latitude)
POINT (16 48)
Why longitude then latitude?
Coordinate system
4
Geometry Model: Region
POLYGON (( x1 y1, x2 y2,
…))
POLYGON((13.59556007
47.47644043, 13.58360004
47.42797852, 13.58401012
47.39070892, 13.60470963
47.29019928, ... ))
OpenGIS Geometry Model
5
Most important: PostGIS or MySQL 5.7
MySQL 5.6 cannot do many geospatial queries ST_Contains, ST_Distance, ST_Within
Drupal 7
Modules to install:
Geofield (with requirements)
Openlayers + OpenLayers UI (with requirements)
Jquery Update
Libraries
Views + Views UI
Chaos tools
Create the content types and fields
Create example data
Create your query
6
Drupal Set-up
7
Create: Node Type Location
8
Create: Node Type Region
9
SQL: Give me all
locations in
Salzburg!
I want MySQL and Drupal
to give me a list of all
locations in the Province of
Salzburg
10
SQL: Give me all locations in Salzburg!
Use a join. To simplify things, the ID of Salzburg is 2008.
SELECT f.entity_id AS entity_id
FROM field_data_field_geographic_middle f
INNER JOIN field_data_field_geographic_area f2
ON ST_Within(GeomFromWKB(field_geographic_middle_geom),
GeomFromWKB(field_geographic_area_geom)) = 1
WHERE (f2.entity_id = 2008)
ORDER BY f.entity_id ASC
11
Drupal: Give me all locations in Salzburg!
Use a Drupal db_select query
Insert the following code into a block.
Put the block in the content region below the main content
<?php
$query = db_select('field_data_field_geographic_middle', 'f');
$query->join('field_data_field_geographic_area', 'f2',
'ST_Within(GeomFromWKB(field_geographic_middle_geom),
GeomFromWKB(field_geographic_area_geom)) = 1');
$query->fields('f', array('entity_id'));
$query->condition('f2.entity_id', 2008);
$query->orderBy('f.entity_id', 'ASC');
?>
12
Drupal: Give me all locations in Salzburg!
… continued
<?php
$resultSet = $query->execute();
print '<hr /><hr />';
foreach ($resultSet as $result) {
$n = node_load($result->entity_id);
$v = node_view($n, 'teaser');
print drupal_render($v) . '<hr />';
}
?>
13
Drupal: Give me all locations in Salzburg!
… continued
<?php
$resultSet = $query->execute();
print '<hr /><hr />';
foreach ($resultSet as $result) {
$n = node_load($result->entity_id);
$v = node_view($n, 'teaser');
print drupal_render($v) . '<hr />';
}
?>
14
What now?
Learn about Geospatial objects in the database
Implement MySQL gepospatial functions in Drupal (no more GeomFromWKB)
http://dev.mysql.com/doc/refman/5.7/en/spatial-extensions.html
Create views plugins
Update list display: geoclustering, map, etc.