Skip to main content

Python functions for geocoding addresses using the Radar API

Project description

Radar Geocoder

Python function for geocoding addresses using the Radar API

This code demonstrates how to use the RadarClient class from the radar module to forward (reverse and ip to be added later on) geocode a list of addresses using the Radar API , extract all the values, and store them in a dataframe.

Prerequisites

You should have a Radar API secret key. If you don't have one, sign up on Radar's website.

https://radar.com/

Install the pandas and radar modules in your Python environment.

pip install pandas
pip install radar-python

Function (Combined Code Below)

Import the pandas library using its alias pd.

import pandas as pd

Import the RadarClient class from the radar module.

from radar import RadarClient

Define a function called f_geocode_address_list that takes a list of addresses and the radar secret key as an arguments.

def f_geocode_address_list(address_list, SECRET_KEY):

Create a RadarClient instance named radar with the secret key stored in the SECRET_KEY variable.

    radar = RadarClient(SECRET_KEY)

Initialize an empty list called geocoded_data to store the geocoded address data.

    geocoded_data = []

Loop through each address in the input address_list using a for loop.

    for address in address_list:

Query the Radar geocoding API for the latitude, longitude, and other relevant data of the current address using the forward method of the radar.geocode object. Store the result in the location variable.

        location = radar.geocode.forward(query=address)[0]

Create a dictionary object called data that stores the relevant fields of the location information.

data = {
            "addressLabel": location.addressLabel,
            "city": location.city,
            "confidence": location.confidence,
            "country": location.country,
            "countryCode": location.countryCode,
            "countryFlag": location.countryFlag,
            "county": location.county,
            "distance": location.distance,
            "formattedAddress": location.formattedAddress,
            "geometryType": location.geometry['type'],
            "layer": location.layer,
            "latitude": location.latitude,
            "longitude": location.longitude,
            "number": location.number,
            "postalCode": location.postalCode,
            "state": location.state,
            "stateCode": location.stateCode,
            "street": location.street
        }

Append the data dictionary to the geocoded_data list.

        geocoded_data.append(data)

Create a pandas DataFrame called geocoded_df from the geocoded_data list using the pd.DataFrame() method.

    geocoded_df = pd.DataFrame(geocoded_data)

Return the DataFrame.

    return geocoded_df

Combined Code

Code for f_geocode_address_list function.

def geocode_address_list(address_list, SECRET_KEY):
    radar = RadarClient(SECRET_KEY)
    geocoded_data = []
    for address in address_list:
        location = radar.geocode.forward(query=address)[0]
        data = {
            "addressLabel": location.addressLabel,
            "city": location.city,
            "confidence": location.confidence,
            "country": location.country,
            "countryCode": location.countryCode,
            "countryFlag": location.countryFlag,
            "county": location.county,
            "distance": location.distance,
            "formattedAddress": location.formattedAddress,
            "geometryType": location.geometry['type'],
            "layer": location.layer,
            "latitude": location.latitude,
            "longitude": location.longitude,
            "number": location.number,
            "postalCode": location.postalCode,
            "state": location.state,
            "stateCode": location.stateCode,
            "street": location.street
        }
        geocoded_data.append(data)
    geocoded_df = pd.DataFrame(geocoded_data)
    return geocoded_df

Installing the RadarGeocoder Package

Install the package using pip.

pip install radargeocoder

Calling The Function (Combined Code Below)

Set a SECRET_KEY variable to a string containing the secret key needed to access the Radar API.

SECRET_KEY = "Your_Key_Here"

Define a list of addresses called address_list.

address_list = ["5 Verti Drive, Waterville, Maine, 04901", "123 Main St, Anytown, California, 12345"]

Call the f_geocode_address_list function with the address_list and SECRET_KEY variable as arguments and store the returned DataFrame in a variable called geocoded_df.

geocoded_df = f_geocode_address_list(address_list, SECRET_KEY)

Display the DataFrame using the geocoded_df variable.

geocoded_df

Combined Code

Combined code for calling the f_geocode_address_list function.

SECRET_KEY = "Your_Key_Here"

address_list = ["5 Verti Drive, Waterville, Maine, 04901", "123 Main St, Anytown, California, 12345"]

geocoded_df = f_geocode_address_list(address_list, SECRET_KEY)

geocoded_df

Example Output.

addressLabel	city	confidence	country	countryCode	countryFlag	county	distance	formattedAddress	geometryType	layer	latitude	longitude	number	postalCode	state	stateCode	street
0	5 Verti Drive	Waterville	exact	United States	US	🇺🇸	Kennebec County	0	5 Verti Drive, Waterville, ME 04901 USA	Point	address	44.513791	-69.660212	5	04901	Maine	ME	Verti Drive
1	123 Main St	San Diego	exact	United States	US	🇺🇸	San Diego County	0	123 Main St, San Diego, CA 12345 USA	Point	address	32.552745	-117.043844	123	12345	California	CA	Main St

License

This code is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

radargeocoder-0.0.10.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

radargeocoder-0.0.10-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file radargeocoder-0.0.10.tar.gz.

File metadata

  • Download URL: radargeocoder-0.0.10.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for radargeocoder-0.0.10.tar.gz
Algorithm Hash digest
SHA256 094ec6b849f20fdd3e6e6d710f658608df9932d8a2a067b7f5fc7e3114a5642c
MD5 14e8132affcbc2848efd16cefe896af7
BLAKE2b-256 0b0c549fcce9f565f56a6a36f489bd7bc4506bbecfab55916e3ca47e690f80a9

See more details on using hashes here.

File details

Details for the file radargeocoder-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: radargeocoder-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for radargeocoder-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 4d50cdc98f567833d221aa96f3c80267d252b43ada438817f5fa511e16d9529d
MD5 db243adb7fac66e35bb599a5a582fcb1
BLAKE2b-256 fc8c9b996b6699615aa8be5ceefe137359b383a7938a845229fe8a4b13b14f39

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page