Geometry data
The Geometry Data service retrieves detailed boundary geometries for places, enabling you to display precise geographic boundaries for countries, regions, cities, neighborhoods, and other geographic areas on your maps.
Basic Usage
import { geometryData } from '@tomtom-org/maps-sdk/services';import { geocode } from '@tomtom-org/maps-sdk/services';
// First, get a place from geocoding or searchconst place = await geocode({ query: 'Amsterdam, Netherlands' });
// Then fetch its geometry dataconst geometry = await geometryData({ geometries: place, zoom: 10 // Appropriate zoom level for the geometry detail});
console.log(geometry.type); // "FeatureCollection"console.log(geometry.features[0].geometry.type); // "Polygon" or "MultiPolygon"Integration with Map Module
Display place boundaries on your map:
import { geometryData } from '@tomtom-org/maps-sdk/services';import { GeometriesModule } from '@tomtom-org/maps-sdk/map';
// Get place and its geometryconst place = await geocode({ query: 'Central Park, New York' });const boundaries = await geometryData({ geometries: place, zoom: 15 });
// Display on mapconst geometriesModule = await GeometriesModule.get(map);geometriesModule.show(boundaries);
// Fit map to boundariesif (boundaries.bbox) { map.mapLibreMap.fitBounds(boundaries.bbox);}Zoom-Level Optimization
Different zoom levels return different levels of detail:
// Low detail for country/region levelconst countryBoundary = await geometryData({ geometries: place, zoom: 5});
// Medium detail for city levelconst cityBoundary = await geometryData({ geometries: place, zoom: 10});
// High detail for neighborhood levelconst detailedBoundary = await geometryData({ geometries: place, zoom: 15});Working with Multiple Geometries
Process multiple places at once:
// Get multiple placesconst places = await search({ query: 'parks', position: [4.9041, 52.3676], limit: 5});
// Get geometries for all placesconst allGeometries = await geometryData({ geometries: places, zoom: 14});
// Display all boundariesgeometriesModule.show(allGeometries);Advanced Styling Options
Customize how boundaries appear on the map:
const geometriesModule = await GeometriesModule.get(map, { fillConfig: { fillColor: '#87CEEB', fillOpacity: 0.3 }, lineConfig: { lineColor: '#4169E1', lineWidth: 2, lineOpacity: 0.8 }});
const boundaries = await geometryData({ geometries: place, zoom: 12 });geometriesModule.show(boundaries);Use Cases
Administrative Boundaries
Display country, state, or city boundaries:
const displayAdminBoundaries = async (placeName, zoomLevel = 8) => { try { const place = await geocode({ query: placeName });
if (place.features.length > 0) { const boundaries = await geometryData({ geometries: place, zoom: zoomLevel });
geometriesModule.show(boundaries);
// Fit map to show the entire boundary if (boundaries.bbox) { map.mapLibreMap.fitBounds(boundaries.bbox, { padding: 50 }); } } } catch (error) { console.error('Failed to display boundaries:', error); }};
// Display Netherlands boundariesawait displayAdminBoundaries('Netherlands', 6);Service Area Visualization
Show delivery or service coverage areas:
const showServiceAreas = async (locations) => { const serviceAreas = [];
for (const location of locations) { const place = await geocode({ query: location.address });
if (place.features.length > 0) { const geometry = await geometryData({ geometries: place, zoom: 12 });
serviceAreas.push({ ...location, boundary: geometry }); } }
// Display all service areas serviceAreas.forEach(area => { geometriesModule.show(area.boundary); });
return serviceAreas;};Geographic Analysis
Compare sizes and overlaps of different areas:
const compareAreas = async (placeNames) => { const areas = {};
for (const name of placeNames) { const place = await geocode({ query: name });
if (place.features.length > 0) { const geometry = await geometryData({ geometries: place, zoom: 8 });
areas[name] = { geometry, bbox: geometry.bbox, area: calculateArea(geometry) // Custom area calculation }; } }
return areas;};
const areas = await compareAreas(['Texas', 'California', 'Alaska']);Performance Considerations
Appropriate Zoom Levels
Choose zoom levels based on your use case:
const getOptimalZoom = (areaType) => { const zoomLevels = { 'country': 4, 'state': 6, 'region': 8, 'city': 10, 'district': 12, 'neighborhood': 14, 'detailed': 16 };
return zoomLevels[areaType] || 10;};
const geometry = await geometryData({ geometries: place, zoom: getOptimalZoom('city')});Caching Geometries
Cache frequently used boundaries:
class GeometryCache { constructor() { this.cache = new Map(); }
getCacheKey(placeId, zoom) { return `${placeId}-${zoom}`; }
async getGeometry(place, zoom) { const placeId = place.features[0]?.properties.id; const key = this.getCacheKey(placeId, zoom);
if (this.cache.has(key)) { return this.cache.get(key); }
const geometry = await geometryData({ geometries: place, zoom }); this.cache.set(key, geometry);
return geometry; }}
const cache = new GeometryCache();const geometry = await cache.getGeometry(place, 10);Error Handling
Handle cases where geometry data isn’t available:
const safeGeometryData = async (place, zoom = 10) => { try { if (!place.features || place.features.length === 0) { return { success: false, message: 'No place data provided' }; }
const geometry = await geometryData({ geometries: place, zoom });
if (!geometry.features || geometry.features.length === 0) { return { success: false, message: 'No geometry data available for this place' }; }
return { success: true, geometry };
} catch (error) { return { success: false, message: `Geometry data request failed: ${error.message}` }; }};Best Practices
- Choose appropriate zoom levels: Higher zoom = more detail but larger file sizes
- Cache geometries: Store frequently accessed boundaries to reduce API calls
- Batch requests: Process multiple geometries together when possible
- Handle missing data: Not all places have detailed boundary information
- Consider file sizes: High-detail geometries can be large - optimize for your use case
- Combine with search: Use search results as input for geometry data requests
Related Guides and Examples
Related Guides
- Search - Find places to get geometry data for
- Geocoding - Convert addresses to places for boundary visualization
- Map Geometries - Display and style boundaries on your map
- Map Places - Comprehensive place display including boundaries
Related Examples
- Multiple Geometries - Display multiple place boundaries simultaneously
- Basic Geometry - Simple geometry display implementation
- Places in Geometry - Find places within specific boundaries
- Geometry Search with POI Categories - Advanced boundary-based search
Related Services
- Places Search - Input source for geometry data requests
- Reverse Geocoding - Get place information from coordinates within boundaries