Geocoding Workflow
This use case demonstrates a typical geocoding workflow: accept user input, find suggestions, geocode an address, and reverse geocode for verification.
Using Python?
The Python SDK wraps all these endpoints in a typed client — client.location.geocode(q="..."), client.location.autocomplete(q="..."), etc.
Scenario
Your application allows users to input addresses. You need to:
- Provide autocomplete suggestions as they type
- Geocode the selected address to coordinates
- Verify the result by reverse geocoding back to an address
Step 1: Address Autocomplete
As the user types, request suggestions:
- cURL
- JavaScript
- Python
# User typed "new y"
curl -X GET "https://graph.quarticle.ro/graph/api/v1/places/autocomplete?q=new%20y" \
-H "Authorization: YOUR_API_KEY"
Response:
{
"results": [
{
"label": "New York, New York",
"lat": 40.7128,
"lon": -74.0060,
"placeId": "abc123",
"geocodingQuality": "city",
"geocodingRequired": false,
"highlights": ["New York"]
},
{
"label": "New Haven, Connecticut",
"lat": 41.3083,
"lon": -72.9279,
"placeId": "def456",
"geocodingQuality": "city",
"geocodingRequired": false,
"highlights": ["New Haven"]
}
]
}
async function getAddressSuggestions(query) {
const response = await fetch(
`https://graph.quarticle.ro/graph/api/v1/places/autocomplete?q=${encodeURIComponent(query)}`,
{
headers: { 'Authorization': `${apiKey}` }
}
);
const { results: suggestions } = await response.json();
// Display suggestions to user
suggestions.forEach(suggestion => {
console.log(`${suggestion.label} (${suggestion.geocodingQuality})`);
});
return suggestions;
}
// Usage
const suggestions = await getAddressSuggestions('new y');
import requests
def get_address_suggestions(query):
headers = {'Authorization': f'{api_key}'}
response = requests.get(
'https://graph.quarticle.ro/graph/api/v1/places/autocomplete',
params={'q': query},
headers=headers
)
data = response.json()
suggestions = data['results']
for suggestion in suggestions:
print(f"{suggestion['label']} ({suggestion['geocodingQuality']})")
return suggestions
# Usage
suggestions = get_address_suggestions('1600 pennsylvania')
Step 2: Geocode Selected Address
User selects an address (or provides a full address). Convert it to coordinates:
- cURL
- JavaScript
- Python
curl -X GET "https://graph.quarticle.ro/graph/api/v1/places/geocode?q=1600%20Pennsylvania%20Avenue%20Washington%20DC" \
-H "Authorization: YOUR_API_KEY"
Response:
{
"result": {
"lat": 38.8951,
"lon": -77.0369,
"label": "1600 Pennsylvania Avenue, Washington, DC 20500",
"placeId": "xyz789",
"geocodingQuality": "rooftop"
}
}
async function geocodeAddress(address) {
const response = await fetch(
`https://graph.quarticle.ro/graph/api/v1/places/geocode?q=${encodeURIComponent(address)}`,
{
headers: { 'Authorization': `${apiKey}` }
}
);
const { result } = await response.json();
console.log(`Address: ${result.label}`);
console.log(`Latitude: ${result.lat}`);
console.log(`Longitude: ${result.lon}`);
console.log(`Quality: ${result.geocodingQuality}`);
return result;
}
// Usage
const location = await geocodeAddress('1600 Pennsylvania Avenue Washington DC');
def geocode_address(address):
response = requests.get(
'https://graph.quarticle.ro/graph/api/v1/places/geocode',
params={'q': address},
headers=headers
)
result = response.json()['result']
print(f"Address: {result['label']}")
print(f"Latitude: {result['lat']}")
print(f"Longitude: {result['lon']}")
print(f"Quality: {result['geocodingQuality']}")
return result
# Usage
location = geocode_address('1600 Pennsylvania Avenue Washington DC')
Step 3: Reverse Geocode for Verification
Verify the geocoded coordinates by converting them back to an address:
- cURL
- JavaScript
- Python
# Use coordinates from previous response
curl -X GET "https://graph.quarticle.ro/graph/api/v1/places/geocode/reverse?lat=38.8951&lon=-77.0369" \
-H "Authorization: YOUR_API_KEY"
Response:
{
"result": {
"label": "1600 Pennsylvania Avenue, Washington, DC 20500",
"placeId": "xyz789",
"geocodingQuality": "rooftop",
"type": "address"
}
}
async function verifyLocation(lat, lon) {
const response = await fetch(
`https://graph.quarticle.ro/graph/api/v1/places/geocode/reverse?lat=${lat}&lon=${lon}`,
{
headers: { 'Authorization': `${apiKey}` }
}
);
const { result: verified } = await response.json();
console.log(`Verified Address: ${verified.label}`);
console.log(`Quality: ${verified.geocodingQuality}`);
return verified;
}
// Usage - verify the location
const verified = await verifyLocation(location.lat, location.lon);
def verify_location(lat, lon):
response = requests.get(
'https://graph.quarticle.ro/graph/api/v1/places/geocode/reverse',
params={'lat': lat, 'lon': lon},
headers=headers
)
verified = response.json()['result']
print(f"Verified Address: {verified['label']}")
print(f"Quality: {verified['geocodingQuality']}")
return verified
# Usage
verified = verify_location(location['lat'], location['lon'])
Complete Workflow Example
- JavaScript
- Python
async function completeGeocodeWorkflow(userInput) {
const apiKey = process.env.QARTA_API_KEY;
try {
// Step 1: Get suggestions
console.log('Getting suggestions...');
const { results: suggestions } = await fetch(
`https://graph.quarticle.ro/graph/api/v1/places/autocomplete?q=${encodeURIComponent(userInput)}`,
{ headers: { 'Authorization': `${apiKey}` } }
).then(r => r.json());
if (suggestions.length === 0) {
console.log('No suggestions found');
return null;
}
// Use first suggestion (or let user select)
const selectedLabel = suggestions[0].label;
console.log(`Selected: ${selectedLabel}`);
// Step 2: Geocode the address
console.log('Geocoding address...');
const { result: location } = await fetch(
`https://graph.quarticle.ro/graph/api/v1/places/geocode?q=${encodeURIComponent(selectedLabel)}`,
{ headers: { 'Authorization': `${apiKey}` } }
).then(r => r.json());
console.log(`Coordinates: [${location.lon}, ${location.lat}]`);
console.log(`Quality: ${location.geocodingQuality}`);
// Step 3: Verify with reverse geocoding
console.log('Verifying location...');
const { result: verified } = await fetch(
`https://graph.quarticle.ro/graph/api/v1/places/geocode/reverse?lat=${location.lat}&lon=${location.lon}`,
{ headers: { 'Authorization': `${apiKey}` } }
).then(r => r.json());
console.log(`Verified: ${verified.label}`);
return {
original: userInput,
selected: selectedLabel,
coordinates: { lat: location.lat, lon: location.lon },
verified: verified.label,
quality: location.geocodingQuality
};
} catch (error) {
console.error('Geocoding error:', error);
return null;
}
}
// Usage
const result = await completeGeocodeWorkflow('new y');
console.log(JSON.stringify(result, null, 2));
def complete_geocode_workflow(user_input):
try:
# Step 1: Get suggestions
print('Getting suggestions...')
data = requests.get(
'https://graph.quarticle.ro/graph/api/v1/places/autocomplete',
params={'q': user_input},
headers=headers
).json()
suggestions = data['results']
if not suggestions:
print('No suggestions found')
return None
selected = suggestions[0]['label']
print(f'Selected: {selected}')
# Step 2: Geocode the address
print('Geocoding address...')
location = requests.get(
'https://graph.quarticle.ro/graph/api/v1/places/geocode',
params={'q': selected},
headers=headers
).json()['result']
print(f"Coordinates: [{location['lon']}, {location['lat']}]")
print(f"Quality: {location['geocodingQuality']}")
# Step 3: Verify with reverse geocoding
print('Verifying location...')
verified = requests.get(
'https://graph.quarticle.ro/graph/api/v1/places/geocode/reverse',
params={'lat': location['lat'], 'lon': location['lon']},
headers=headers
).json()['result']
print(f"Verified: {verified['label']}")
return {
'original': user_input,
'selected': selected,
'coordinates': {'lat': location['lat'], 'lon': location['lon']},
'verified': verified['label'],
'quality': location['geocodingQuality']
}
except Exception as e:
print(f'Error: {e}')
return None
# Usage
result = complete_geocode_workflow('new y')
print(json.dumps(result, indent=2))
Use Cases
This workflow is useful for:
- Address input forms - Guide users with suggestions
- Mapping applications - Convert addresses to map coordinates
- Location validation - Verify user input is a real address
- Multi-step flows - Combine with portfolio analysis using the coordinates
- Quality assurance - Check geocoding quality before storing
Error Handling
async function geocodeWithErrorHandling(address) {
const response = await fetch(
`https://graph.quarticle.ro/graph/api/v1/places/geocode?q=${encodeURIComponent(address)}`,
{ headers: { 'Authorization': `${apiKey}` } }
);
if (response.status === 400) {
console.error('Invalid address format');
return null;
}
if (response.status === 401) {
console.error('Invalid API key');
return null;
}
const { result } = await response.json();
// Check geocoding quality
if (result.geocodingQuality === 'address' || result.geocodingQuality === 'rooftop') {
console.log('High quality match');
return result;
} else {
console.warn(`Low quality match (${result.geocodingQuality})`);
return result;
}
}
Next Steps
- Portfolio Analysis Use Case - Use geocoded coordinates for spatial analysis
- Spatial Data Guide - Work with geographic shapes
- Postman Collection - Browse all endpoints