Concepts / JavaScript for Web Visualization

JavaScript for Web Visualization

A geospatial application pipeline connects input data, a geocoding API, a database, and a visualization layer.

  • Programming

From Names to Map Points

A web map cannot plot a phrase such as "University of Michigan" directly. The phrase must first become structured geographic data. In this project, raw location names travel through several specialized components: an input file, the OpenStreetMap geocoding API, a SQLite database, and a JavaScript visualization layer. Each component performs one part of the overall task.

location stringscoordinates and recordsdatabase recordsJavaScript datawhere.dataraw location namesOpenStreetMap APIgeographic datageodata.sqlitestored recordswhere.jsexecutable JavaScriptWeb maplocation markers
How does location data move from the input file through geocoding, database storage, extraction, and JavaScript visualization?

The important idea is not JavaScript alone. The visualization works because earlier stages have cleaned, enriched, stored, and formatted the location data so that JavaScript can use it.

Geocoding the Location

Geocoding converts a human-readable location name into structured geographic information. The OpenStreetMap geocoding API interprets the submitted text and returns geographic data that includes latitude and longitude coordinates, a standardized address, and other metadata. Latitude and longitude are the fields that make it possible to place the location on a map.

Raw location strings are not always consistent. A person might enter "UMich", "University of Michigan", or "Ann Arbor". These strings differ in wording, yet a geocoding service can interpret them and return machine-readable geographic results. Without this service, each location would need to be looked up and entered manually.

sendreturnreturnreturnUniversity ofMichiganraw location stringGeocoding requestOpenStreetMap APILocation nameUniversity of MichiganLatitude42.2656Longitude-83.7430
What changes when a raw location string is sent to the geocoding API, and what structured geographic fields come back?

A University Name Becomes Coordinates

A researcher wants to plot University of Michigan on a map.

Submit the name: The location string University of Michigan is sent to the OpenStreetMap geocoding API.

Receive structured data: The API returns latitude 42.2656 and longitude -83.7430 for the location.

Prepare for storage: The location name and returned geographic values can now be stored as a database record.

A human-readable location has become a record containing a name, latitude, and longitude.

Loading Records with geoload.py

geoload.py coordinates the loading stage. It reads location names line by line from where.data. For each name, it checks geodata.sqlite. If a record already exists, the program skips another API request. If the location is not present, geoload.py calls the OpenStreetMap geocoding API and stores the new geographic record in the database.

locationalready storednot storedreturned dataRead locationwhere.dataCheck databasegeodata.sqliteSkip requestrecord existsCall APInew locationStore recordname, latitude, longitude
How does geoload.py read input locations, call the geocoding API, and store returned geographic records?

Two Input Lines

A where.data file contains University of Michigan followed by UMich.

Process the first line: geoload.py finds no University of Michigan record in geodata.sqlite, calls the API, receives latitude 42.2656 and longitude -83.7430, and stores the result.

Process the second line: geoload.py checks for UMich. The source scenario says the API recognizes it as a variant of University of Michigan and returns the same coordinates.

Store the second result: The source scenario describes storing UMich as a separate record, while noting that smarter logic could deduplicate it.

The loader turns each input line into a database record unless the location is already stored.

Exporting Data with geodump.py

After geoload.py has populated geodata.sqlite, geodump.py creates the bridge to the web page. It reads the database records, including the location name, latitude, and longitude, and writes them to where.js. The output is executable JavaScript rather than an unprocessed database export, so it can be included directly in an HTML page.

readwrite formatted dataincludeDatabase recordsname, latitude, longitudegeodump.pyextract and formatwhere.jsvar locationsHTML pageJavaScript included
How does geodump.py retrieve database records and transform them into data that JavaScript can use?

The source scenario describes where.js containing data in a form like var locations = [{lat: 42.2656, lng: -83.7430, name: 'University of Michigan'}, ...]. This format gives the web page a collection of location objects with the fields needed to display map markers.

geodump.py does not perform the original geocoding. That work has already happened during loading. Its responsibility is to extract persistent records and transform them into executable JavaScript for the visualization layer.

Connecting Data to the Map

The visualization layer receives the data exported by geodump.py. When where.js is included in an HTML page alongside a mapping library, the JavaScript data is available to the page. The map can then use each record's latitude and longitude to display the corresponding location as a marker.

containsexported aspositionsgeodata.sqlitepersistent recordsLocation recordname, latitude, longitudewhere.jsJavaScript objectMap markergeographic position
What data does the visualization layer receive from the database, and how is that data connected to the geographic display?

The complete sequence is therefore: input names are geocoded, geographic records are persisted, records are exported as JavaScript, and the web page uses the exported values to display locations. The visualization is the final stage of a pipeline rather than an isolated script.

Keep the responsibilities separate. geoload.py handles input and API integration, the database stores records persistently, geodump.py handles export and transformation, and HTML and JavaScript handle visualization. With this arrangement, changing the visualization library does not require changing the data-loading process.

Mistakes in the Pipeline

  • Treating the raw location string as if it were already map data.

    A human-readable name does not by itself provide the latitude and longitude needed to position a marker.

    Fix: Send the location to the geocoding API and use the returned structured geographic data.

  • Calling the API every time geoload.py encounters a location.

    This creates redundant API calls and does not use the database as persistent storage.

    Fix: Check the database first and call the API only for locations that are not already stored.

  • Expecting the database to be directly usable by the web page.

    The project uses geodump.py to transform database records into the JavaScript file where.js.

    Fix: Run geodump.py and include the resulting where.js in the HTML page.

  • Making geodump.py responsible for geocoding.

    Geocoding belongs to the loading stage. geodump.py reads existing records and formats them for JavaScript.

    Fix: Use geoload.py for input and API integration, then use geodump.py for extraction and formatting.

Trace the Data Yourself

MEDIUM

A new location appears in where.data, but it is not yet in geodata.sqlite. Trace the stages it passes through before a map marker can appear. Name the responsible component at each stage and identify the geographic fields that must survive into the visualization data.

Hints
  • Begin with the input file and ask which program reads it.
  • Identify the condition that determines whether an API call is made.
  • Follow the stored record into the export file.
  • The visualization needs the location name, latitude, and longitude.

What do you think happens?

If geoload.py finds a location already stored in geodata.sqlite, what should happen next?

  • Call the geocoding API again
  • Skip the redundant API call
  • Send the record directly to the map
  • Delete the existing record
Reveal answer

Answer: Skip the redundant API call

geoload.py checks the database before making a request. Existing records do not need to be geocoded again.

Pipeline Summary

  1. The OpenStreetMap geocoding API transforms human-readable location strings into structured geographic data, including latitude and longitude.
  2. geoload.py reads where.data, checks geodata.sqlite, avoids redundant API calls, and stores new geographic records.
  3. geodump.py reads the persistent database records and writes executable JavaScript to where.js.
  4. The HTML and JavaScript visualization layer uses the exported location data to display map markers.
  5. The separation of input, API integration, storage, export, and visualization makes the application modular and easier to change.

Key Takeaways

  • A geospatial visualization is a pipeline connecting raw input, geocoding, persistent storage, data export, and web display.
  • The geocoding API supplies the structured geographic fields that raw names lack.
  • geoload.py populates the database while avoiding repeated API calls for existing locations.
  • geodump.py converts database records into executable JavaScript in where.js.
  • The visualization layer can display map markers because the earlier pipeline stages prepared the location data.