Other Ways of Displaying Spatial Data

Sometimes a map is not the most useful way to display spatial data. You may want to present information as a list, either on its own or alongside the same data on a map. Or it may be necessary to create an external feed so that your data can be pulled out into another service, such as Google Earth or a different website. Because maps made in Drupal are generally built using the Views module, it is a fairly simple process to output data in other formats.

Spatial Data in Tables and Lists

Foursquare on a mobile device is an example where it is more useful to have a list of nearby venues such as cafes, parks, or airports. Continuing with our example of upcoming Drupal events, it may be useful to see a list of events sorted by date, rather than a map of all of them. With the Views module, both of these are just List Views of the Map Views that we created earlier.

  1. On the Views page, add a new View.

  2. For the “View name,” enter Upcoming events.

  3. Enter a description of A table of upcoming events.

  4. In the “Show” section, leave “Content” selected from the first drop-down. Change the “type” drop-down to “Event.” Change the “sorted by” drop-down to “Unsorted.”

  5. Leave “Create a page” checked. This is the page that will contain the table of events. In the “Display format” section, change the first drop-down to “Table.” Change “Items to display” to 100.

  6. Click the “Continue & edit” button.

  7. In the “Fields” section, click the “add” button. From the list of available fields, select “Content: Event date” and click “Add and configure fields.” In the “Choose how users view dates and times” field, select the “Medium” format. You could create custom date formats using the link below that field, but this is not necessary. Click the “Apply (all displays)” button.

  8. In the “Fields” section, click the “add” button. Select “Content: Event location” from the list of available fields and click “Add and configure fields.” Click the “Apply (all displays)” button.

  9. In the “Fields” section, click the “add” button. Select “Content: Body” from the list of available fields and click “Add and configure fields.” Change the “Label” field to Description. Expand the “Rewrite Results” section. Tick the “Trim this field to a maximum length” checkbox. Enter 100 as the maximum length. Leave the “Trim only on a word boundary” and “Add an ellipsis” checkboxes ticked. Again, click the “Apply (all displays)” button.

  10. Below the view configuration form is a preview of the table that you are creating. The header of the table displays the field labels, but the header for the event name is missing. To fix this, click on the “Content: Title” link in the “Fields” section. Tick the “Create a label” checkbox. Enter Event in the “Label” field. Click the “Apply (all displays)” button.

  11. To make a more useful interface, we can take advantage of some of the geographic information that is associated with these events. A list of countries will be added to the page, which people can use to find events just in a particular country. From the “Filter criteria” section click the “add” button. From the list of available filters select “Content: Event location (field_event_location:country)” and click the “Add and configure filter criteria” button. Tick the “Expose this filter to visitors, to allow them to change it” checkbox. Change the “Label” field to Event country. Leave the “Operator” settings with the default values. Click the “Apply (all displays)” button.

  12. The events now need to be sorted by their date. From the “Sort criteria” section click the “add” button. Select “Content: Event date (field_event_date)” from the list of available sort criteria and click the “Add and configure sort criteria” button. Leave “Sort ascending” selected. Click the “Apply (all displays)” button.

  13. The user might select a country such as Antarctica that has no events, so we could provide some text for when this happens. The “Advanced” section of the view configuration form has a “No results behavior” section. Here, click the “add” button. Select “Global: Text area” from the available behaviors. Click the “Add and configure” button. In the main text field, enter some text like There are no events listed in that country. Click the “Apply (all displays)” button.

  14. Click the “Save” button on the top right to create the view. It should look something like Figure 4-14.

    Table view of geographic data

    Figure 4-14. Table view of geographic data

The example above is a fairly simple one, and there are many other things that can be done with Views. A list or table view can be attached to a map view using a Views attachment, so that users can see a list of locations below the map.

Proximity is a useful piece of data to have. If you are using the Location module, its Views integration includes a proximity field (how close is this place to another place), a proximity filter (show me only things that are within 10 miles of this place), a proximity argument (like a proximity filter that can have the location changed through code), and sort handlers (do you want to see the nearest or furthest places first). There is also support for calculating the proximity to post codes, although this is only supported for a few countries and requires post code data to be loaded into your database. This process is outlined in the INSTALL.txt file of the module and on the Location handbook page.

There is work in progress on an OpenLayers Proximity module for Drupal 7. This will provide an Exposed Filter that will allow a user to enter their location and see a list of nearby content with distances.

Creating Feeds

In Creating an OpenLayers Map Layer from KML, a KML file was taken from an external source and displayed on a map. With Drupal it is easy to create a KML file like this for other sites to use. The KML module integrates with both the Location and Geofield modules; it just requires a latitude and longitude field. This example will extend the View created in Spatial Data in Tables and Lists to create a KML file that includes Drupal events that people have added to the website, such as meetups, happy hours, and conferences.

  1. Download and install the latest stable release of the KML module. There is no permissions change or other configuration needed for this module.

  2. Edit the Upcoming events view at admin/structure/views/view/upcoming_events/edit.

  3. At the top of the page click the “+ Add” button and add a “Feed.”

  4. The fields in this Feed Display will be different from the fields in the Page Display that we created before. The Title field will no longer be a link, because we do not have any particular need to bring people to our event details page. More importantly we need to include the latitude and longitude fields in this Display. Be sure to override the fields, otherwise the Page Display (the table we created earlier) will be changed, too.

    In the Fields section, edit the Title field. At the top of that screen, change the “For” drop-down from “All displays” to “This feed (override).” In the “Rewrite results” section, untick the “Output this field as a link” checkbox.

  5. Add a new field, “Content: Event geo.” In the configuration for this field, add a label of Longitude. In the “Formatter” drop-down, change to “Longitude only.” Leave the “Format” as “Decimal degrees.”

  6. Repeat the previous step, setting the label and formatter to Latitude.

  7. On the Feed details page, change the Format from “RSS Feed” to “KML Feed.”

  8. On the “Feed: Style options” screen, the “Field Usage” section is where the XML elements for the KML file are matched with fields in the Drupal View. Set the Name to “Content: Title,” the Description to “Description,” the Longitude to “Longitude,” and the Latitude to “Latitude.” Change the Filename to drupal-events. Click the “Apply” button to save.

  9. In the Feed settings section, update the path to drupal-events.kml.

  10. All of the other view settings will be inherited by the KML feed from the Page display, and can be left unchanged. Save the changes to the View.

  11. You can now download the KML file at /drupal-events.kml (or the path you set for the feed). The KML file can be viewed in Google Earth or uploaded to many different mapping services.

The Views module has caching that can be configured in the Advanced section of the Views interface. One fast and efficient way to display maps in Drupal is to create a KML file in Views, which is automatically cached; pulling that KML file into a map in OpenLayers is outlined in Creating an OpenLayers Map Layer from KML.

Get Mapping with Drupal now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.