Concepts / Building Interactive Maps with OpenStreetMap and Leaflet

Building Interactive Maps with OpenStreetMap and Leaflet

An interactive map is the final stage of a data pipeline.

  • Programming

From Stored Places to Visible Markers

An interactive map is the final stage of a data pipeline. The location information begins in a SQLite database, geodump.py extracts the relevant fields, and the extracted values are written into where.js as executable JavaScript. where.html loads that file, and the browser uses the data to render the interactive map. Understanding this path makes it easier to see both how the map works and where to look when a location is missing.

SQLite databaselocation recordsgeodump.pyextract and formatwhere.jsnested arrayswhere.htmlloads map dataBrowser mapvisible locationsrecordswritesloaded byrenders
How does location data move from the database through geodump.py and where.js into the interactive map?

The Location Record Shape

where.js represents geographic data as a list of lists. Each inner list describes one location and contains exactly three elements: the location name, its latitude, and its longitude. The position of each element matters. The first element identifies the place, the second gives the latitude, and the third gives the longitude. The outer list groups all of the locations together.

One locationnested listNamelocation nameLatitudenumberLongitudenumberelement 1element 2element 3
What does each nested array contain, and how are the name, latitude, and longitude connected?
javascript

What geodump.py Changes

The database and the browser do not use the location data in the same form. geodump.py performs the conversion between them. It extracts the location name, latitude, and longitude from database records, then formats those values as nested arrays in where.js. Because the result is executable JavaScript code, the browser can load it as map data rather than treating it as an unrelated text report.

Location recordname, latitude, longitudeNested arrayJavaScript datawhere.jsexecutable codegeodump.py formatswritten to
What changes when a database record is extracted and formatted as executable JavaScript?
Worked example

Following One Location

Trace the representation of one location through the conversion step.

  1. 1
    Start with the database record

    The record supplies three relevant values: a location name, a latitude, and a longitude.

  2. 2
    Extract the geographic fields

    geodump.py takes those values from the database record.

  3. 3
    Format the result

    The values are placed in one inner list, with the name first, latitude second, and longitude third.

  4. 4
    Write the JavaScript file

    The inner list becomes part of the nested-array data in where.js.

Result

One database location becomes one nested JavaScript array that the browser can use as map data.

How the Browser Uses where.js

After geodump.py has produced where.js, the browser-side part of the pipeline begins. where.html loads the JavaScript file, and the map process reads the location arrays. For each inner list, the name identifies the place while the latitude and longitude identify its geographic position. Leaflet can then use those coordinates to represent the location as a visible map marker in the interactive map.

Location arrayname, latitude, longitudeCoordinateslatitude and longitudeLeafletmap renderingMap markervisible locationreads positionsupplies locationrenders
How does Leaflet read the generated geographic data and turn each coordinate into a visible map marker?
javascript

In this illustrative example, the outer array contains one location. The inner array connects the name Library with its latitude and longitude. A Leaflet-based map can use the coordinate pair to place the location and the name to identify it.

When Locations Do Not Appear

A missing marker can result from a problem at different points in the pipeline. Begin with the browser and work backward. First open where.html in a browser. If the expected map or locations do not appear, inspect the developer console for errors. Then verify that where.js exists in the same directory as the HTML file. These checks distinguish a browser-loading problem from a missing generated data file.

Open where.htmlbrowserMap visiblecheck displayDeveloper consolecheck errorswhere.jssame directoryInteractive maplocations visibleloadsif not visibleverify data fileavailable to page
Where in the database-to-browser flow can a location disappear or fail to appear on the map?
  • Common mistake

    Looking only at the map instead of checking the developer console.

    What it looks like

    The page appears empty, so the problem is assumed to be the map itself.

    Why it fails

    The console can reveal errors that explain why the browser did not load or process the map data.

    Do this instead

    Open the developer console as an early troubleshooting step.

  • Common mistake

    Placing where.js in a different directory from where.html.

    What it looks like

    where.html is opened, but the generated data file is stored elsewhere.

    Why it fails

    The documented visualization setup requires where.js to exist in the same directory as the HTML file.

    Do this instead

    Verify that where.js is present beside where.html.

  • Common mistake

    Misreading the order inside a location array.

    What it looks like

    Treating the first element as latitude instead of the location name.

    Why it fails

    Each inner list is ordered as name, latitude, and longitude.

    Do this instead

    Read element positions in that order.

Trace the Pipeline

MEDIUM

A location is stored in the database with a name, latitude, and longitude. Describe the four stages that carry it to the browser map, and state what you would check first if the location were not visible.

Hints
  • Start with the SQLite database and identify the program that extracts the fields.
  • Remember the order of values inside one where.js location list.
  • For troubleshooting, begin with where.html and the developer console.
  1. 1
    The complete flow is SQLite database to geodump.py to where.js to where.html and the browser map. geodump.py extracts location names, latitudes, and longitudes and formats them as executable JavaScript. where.js stores the data as a list of lists, with each inner list ordered as name, latitude, and longitude. Leaflet uses the geographic coordinates to represent locations on the interactive map. If nothing appears, open where.html in a browser, check the developer console, and confirm that where.js is in the same directory.

Key Takeaways

  • geodump.py converts database location records into executable JavaScript data.
  • where.js stores geographic information as nested arrays containing name, latitude, and longitude.
  • The pipeline runs from SQLite through Python and JavaScript into where.html and the browser map.
  • Leaflet uses the latitude and longitude values to represent locations visually.
  • For missing locations, check the developer console and verify that where.js is beside where.html.

Related concepts

More concepts starting with B