Concepts / Storing Geographic Data in Databases

Storing Geographic Data in Databases

Deleting geodata.sqlite is not a selective refresh.

  • Programming

The Cost of Repeating a Lookup

A geocoding program looks up a location by sending a request to an external geocoding API. Every lookup consumes an API call, and most geocoding services impose a daily limit on the number of requests. If the same location is processed again, making another request wastes one of those limited calls.

Definition

A cache is local storage for results that have already been retrieved. In this workflow, SQLite acts as a local cache for geocoding results. Before contacting the API, geoload.py checks whether the location is already stored in geodata.sqlite. If it is stored, the program skips the API call.

Key point

The central pattern is check the cache, call the API only when necessary, and store the result for later use.

Locationinput listCache checkgeodata.sqliteGeocoding APIonly if not cachedGeocoding resultstored locallyCached resultfuture lookupreadnot storedreturnssavealready stored
How does a location move from the input list to the SQLite cache, and how does a later lookup avoid making another API call?

The First Run

On the first run, the database may be empty. geoload.py reads a location from the input file and checks geodata.sqlite. Because the location is not present, the program calls the geocoding API. It then stores the returned geocoding result in the database. The next time the same location appears, the stored result can be used instead of making another API request.

Worked example

Ten Locations on the First Run

Imagine that an input file contains ten locations and geodata.sqlite does not yet contain any of them. What happens during processing?

  1. 1
    Read a location

    geoload.py reads the first location from the input file.

  2. 2
    Check the database

    The program checks geodata.sqlite and finds no cached result for that location.

  3. 3
    Make the API request

    Because the location is not cached, the program calls the geocoding API.

  4. 4
    Store the result

    The returned geocoding result is stored locally in the database.

  5. 5
    Continue processing

    The program repeats the same check, request, and storage pattern for the remaining locations.

Result

All ten locations require API calls in this first-run scenario, and their results are stored for later runs.

Read locationinput fileCheck databasegeodata.sqliteSkip API callcached locationCall APIuncached locationStore resultlocal databaseNext locationcontinue inputlocation loadedcachednot cachedresult returnedcontinuecontinue
What happens next as geoload.py reads a location, checks the database, calls the geocoding API when needed, and stores the result?

A Faster Second Run

Caching becomes especially valuable when a later input file contains locations that were already processed. geoload.py checks each location against the existing database. Cached locations are skipped, while new locations follow the API-request path and are stored after their results arrive.

Worked example

Ten Existing Locations and Five New Locations

A first run processed ten locations. A second input file contains those same ten locations plus five new locations. How many API calls are needed on the second run?

  1. 1
    Inspect the original ten

    The program checks each of the original ten locations and finds them in geodata.sqlite.

  2. 2
    Skip cached locations

    Because those ten results are already stored, the program does not call the API for them.

  3. 3
    Reach the five new locations

    The five new locations are not found in the database.

  4. 4
    Request and store new results

    The program calls the API for the five new locations and stores their results.

Result

The second run makes five API calls instead of fifteen. The source example describes this second run as five times faster than the first because the ten original locations are served from the cache.

Ten locationsfirst runFifteen locationslater runTen API callsresults storedTen cached locationsAPI skippedFive new locationsAPI requiredFive API callsnew results storedall uncachedalready storednot storedrequest
What changes between the first run and a subsequent run when some locations are already stored in geodata.sqlite?
Key point

The cache does not eliminate all API calls forever. It eliminates repeated calls for locations whose results are already stored. New locations still require requests the first time they are encountered.

Working Within Daily Limits

A daily API rate limit is a maximum number of requests allowed during a 24-hour period. A small dataset may fit within one run, but a dataset containing thousands of locations may require more requests than the daily limit allows.

geoload.py includes a counter that can limit how many API calls are made in one run. For example, the counter can be set to 100 calls per run. The program processes uncached locations until it reaches that limit, stores the results it has obtained, and stops. A later run can continue with the next uncached locations because the earlier results are now in the database.

Counter at zeronew runAPI requestuncached locationCounter increasesone call recordedCall limitfor this runNext uncachedlocationcontinue processingRun stopsresume lateruncached workcomplete callcheck counterbelow limitlimit reached
How does the API request counter change during processing, and what control-flow decision occurs when the limit is reached?
RunWhat happensWhy the next run can continue
First runProcesses new locations until the counter limit is reachedCompleted results are stored in geodata.sqlite
Later runChecks the database and processes the next uncached locationsPreviously completed locations are skipped
Following runsRepeat the same batch process over several daysThe cache records the progress already made

The counter and persistent local cache work together to divide a large dataset into manageable batches.

Understanding the Stored Result

The database connects a location with the geocoding result retrieved for that location. The important behavior is not that the program stores a particular visual map or makes a new request every time. The important behavior is that a previously retrieved result is stored locally and can be found when the same location is processed again.

Locationlookup inputAPI responseretrieved resultStored coordinatespart of the resultCached resultgeodata.sqlitelookupcontainsstored with resultstored locally
What does a cached location connect to when geoload.py stores a geocoding result?

The diagram shows the conceptual relationship rather than a complete SQLite schema. The source material establishes that locations and their geocoding results are stored locally, including stored coordinate information, but it does not specify every database column.

Resetting the Cache

Sometimes the existing cache should be discarded. Removing the geodata.sqlite file clears all stored results. When geoload.py is run afterward, it does not find the old database, creates a new one, and treats every location as uncached. The program therefore needs to geocode the locations again from scratch.

  • Remove geodata.sqlite when you want to clear all cached data.
  • Understand that deletion erases every stored result in the cache.
  • Expect the next run to make fresh API calls for all processed locations.
  • Use this reset when cached data may be incorrect, when switching to a different geocoding service, or when testing the program.
Stored resultsgeodata.sqliteDelete databasecache clearedNew databasecreated on next runFresh API callslocations treated asuncachedremove filenext runno old results found
What state is lost when geodata.sqlite is deleted, and how does the next run return to making fresh API calls?

Mistakes to Avoid

  • Common mistake

    Assuming every location requires an API call on every run.

    What it looks like

    A later input file contains locations that are already present in geodata.sqlite, but the program is treated as though the database does not matter.

    Why it fails

    The cache exists specifically to prevent redundant calls for locations whose results have already been stored.

    Do this instead

    Check the database before requesting data from the API.

  • Common mistake

    Using the request counter as though it permanently records all future progress.

    What it looks like

    A run is limited to 100 calls, and the counter is expected to replace the database.

    Why it fails

    The counter limits calls for a run, while the database preserves completed results between runs.

    Do this instead

    Use the counter to control the current batch and geodata.sqlite to remember cached results.

  • Common mistake

    Deleting geodata.sqlite without recognizing the consequence.

    What it looks like

    The database is removed to solve a problem, but the next run is expected to skip the old locations.

    Why it fails

    Removing the file erases all stored results.

    Do this instead

    Delete the file only when you intentionally want a fresh cache and fresh geocoding.

  • Common mistake

    Trying to process thousands of locations in one unrestricted run.

    What it looks like

    A large dataset is sent through the program without considering the service's daily request limit.

    Why it fails

    Most geocoding APIs impose a maximum number of requests in a 24-hour period.

    Do this instead

    Set a per-run counter and process the dataset in batches over multiple days.

Check Your Understanding

EASY

A first run processes 100 locations and stores their results. The next input file contains 100 of those locations and 20 new locations. Explain which locations need API calls and why.

Hints
  • Start by separating locations already stored in geodata.sqlite from locations not found there.
  • Remember that the cache is checked before the API is called.
  • The new locations follow the request-and-store path.
MEDIUM

A dataset contains thousands of locations, and the program is configured for 100 API calls per run. Describe what should happen after the first run reaches its limit and what the next run should do.

Hints
  • The completed results remain in the local database.
  • The next run checks the database before making new requests.
  • The process can continue over several days.

What do you think happens?

What happens if geodata.sqlite is removed before running geoload.py again?

  • All locations are treated as already cached

  • The program creates a new database and treats locations as uncached

  • Only the newest cached location is removed

  • The API counter is permanently disabled

Reveal answer
Answer:

The program creates a new database and treats locations as uncached.

Removing geodata.sqlite erases all stored results. On the next run, geoload.py does not find the old database, creates a new one, and must re-geocode the locations.

The Repeatable Pattern

SQLite turns geocoding into a repeatable workflow instead of a sequence of independent API requests. geoload.py checks the local database first, requests data only for locations that are not cached, and stores new results for later runs.

  1. 1
    A local SQLite database prevents redundant API calls by storing geocoding results.
  2. 2
    The program checks the cache before calling the API for each location.
  3. 3
    A request counter limits calls per run, allowing large datasets to be processed in batches over several days.
  4. 4
    Deleting geodata.sqlite clears every cached result and causes the next run to start fresh.
  5. 5
    The pattern of checking local results, fetching only when needed, and storing the result applies to other external-data workflows.

Key Takeaways

  • SQLite acts as a local cache for geocoding results.
  • geoload.py avoids repeated API calls by checking geodata.sqlite before contacting the API.
  • A per-run request counter and repeated runs help handle daily API rate limits.
  • Removing geodata.sqlite erases the cache and forces fresh geocoding on the next run.
  • The overall strategy is check, fetch only when needed, and store.

Related concepts

More concepts starting with S