14
Geospatial database queries in Drupal 7 using the GeoField Module Jeremy Chinquist 1

Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

Geospatial database queries in

Drupal 7 using the GeoField Module

Jeremy Chinquist

1

Page 2: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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)

Page 3: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

Geometry Model: Point

3

POINT (x y)

POINT (longitude latitude)

POINT (16 48)

Why longitude then latitude?

Coordinate system

Page 4: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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, ... ))

Page 5: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

OpenGIS Geometry Model

5

Page 6: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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

Page 7: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

7

Create: Node Type Location

Page 8: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

8

Create: Node Type Region

Page 9: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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

Page 10: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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

Page 11: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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');

?>

Page 12: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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 />';

}

?>

Page 13: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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 />';

}

?>

Page 14: Geospatial database queries in Drupal 7 using the GeoField ... · Drupal 7 Modules to install: Geofield (with requirements) Openlayers + OpenLayers UI (with requirements) Jquery Update

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.