Geocoding? Adding coordinates column to a dataset of spots

72 Views Asked by At

If I have a dataset with industries names and cities where they’re placed, is there a way I can get addresses or coordinates automatically from google maps? I tried asking ChatGPt that told me to use Geoocoding.

I need to create a column in the dataset with the address or the coordinates so I can geolocate them on a map. I only have the cities column.

Example: Smaltimento Rifiuti Ecoiso | Napoli. I need two other columns with the coordinates taken from Google Maps or another with the address (better the first ones)

Tried getting Google apis

1

There are 1 best solutions below

0
Bench Vue On

This code uses the Google Places API to fetch the address and Geolocation (latitude and longitude) for various industries in different cities. It iterates over a dataset containing industry names and cities, makes API requests to retrieve place IDs and details, and then logs the results with address and location information. The goal is to gather geographic data for each industry-city pair using the Google Places API

One Google API

You need to enable one Google API Places API

enter image description here

You need to copy API Key from your project of Google account.

https://console.cloud.google.com/cloud-resource-manager

enter image description here

More detail information in here

Demo code

This code will address your question.

Save as get-address.js

const axios = require('axios');

const API_KEY = 'Your Google Maps API key';

// your data sat
const data = [
    { industry: 'Smaltimento Rifiuti Ecoiso', city: 'Napoli' },
    { industry: 'Automotive Manufacturing', city: 'Turin' },
    { industry: 'Fashion Design', city: 'Milan' },
    { industry: 'Wine Production', city: 'Florence' },
    { industry: 'Maritime Shipping', city: 'Genoa' },
    { industry: 'Tourism Hospitality', city: 'Venice' },
    { industry: 'Food Processing', city: 'Bologna' },
    { industry: 'Ceramics Production', city: 'Faenza' },
];

async function getPlaceDetails(placeId) {
    try {
        const response = await axios.get('https://maps.googleapis.com/maps/api/place/details/json', {
            params: {
                place_id: placeId,
                key: API_KEY
            }
        });

        if (response.data.result) {
            const { formatted_address, geometry } = response.data.result;
            const { lat, lng } = geometry.location;
            return { address: formatted_address, location: { latitude: lat, longitude: lng } };
        } else {
            throw new Error('No details found for place ID: ' + placeId);
        }
    } catch (error) {
        throw new Error('Error fetching place details: ' + error.message);
    }
}

async function searchPlaceId(cityName, industryName) {
    try {
        const response = await axios.get('https://maps.googleapis.com/maps/api/place/textsearch/json', {
            params: {
                query: industryName + ' in ' + cityName,
                key: API_KEY
            }
        });

        if (response.data.results.length > 0) {
            const placeId = response.data.results[0].place_id;
            return placeId;
        } else {
            throw new Error('No results found for query: ' + industryName + ' in ' + cityName);
        }
    } catch (error) {
        throw new Error('Error fetching place ID: ' + error.message);
    }
}

async function getAddressAndLocationForIndustryAndCity(industryName, cityName) {
    try {
        const placeId = await searchPlaceId(cityName, industryName);
        const { address, location } = await getPlaceDetails(placeId);
        return { industry: industryName, city: cityName, address, location };
    } catch (error) {
        throw new Error('Error getting address and location: ' + error.message);
    }
}

async function fetchData(data) {
    const results = [];
    for (const { industry, city } of data) {
        try {
            const result = await getAddressAndLocationForIndustryAndCity(industry, city);
            results.push(result);
        } catch (error) {
            console.error(`Error processing ${industry}, ${city}: ${error.message}`);
        }
    }
    return results;
}

fetchData(data)
    .then(results => {
        console.log('Address and location for each industry:');
        console.log(results);
    })
    .catch(error => {
        console.error('Error:', error.message);
    });

Input data

[
    { industry: 'Smaltimento Rifiuti Ecoiso', city: 'Napoli' },
    { industry: 'Automotive Manufacturing', city: 'Turin' },
    { industry: 'Fashion Design', city: 'Milan' },
    { industry: 'Wine Production', city: 'Florence' },
    { industry: 'Maritime Shipping', city: 'Genoa' },
    { industry: 'Tourism Hospitality', city: 'Venice' },
    { industry: 'Food Processing', city: 'Bologna' },
    { industry: 'Ceramics Production', city: 'Faenza' }
]

Output

[
  {
    industry: 'Smaltimento Rifiuti Ecoiso',
    city: 'Napoli',
    address: "Cupa Vicinale Sant'Aniello, 96, 80146 Napoli NA, Italy",
    location: { latitude: 40.82747369999999, longitude: 14.3224339 }
  },
  {
    industry: 'Automotive Manufacturing',
    city: 'Turin',
    address: "S.da Comunale da Bertolla all'Abbadia di Stura, 132, 10156 Torino TO, Italy",
    location: { latitude: 45.1095563, longitude: 7.7345843 }
  },
  {
    industry: 'Fashion Design',
    city: 'Milan',
    address: 'Via Giuseppe Broggi, 7, 20129 Milano MI, Italy',
    location: { latitude: 45.4781573, longitude: 9.2103889 }
  },
  {
    industry: 'Wine Production',
    city: 'Florence',
    address: 'Via dei Pepi, 22, 50122 Firenze FI, Italy',
    location: { latitude: 43.77029899999999, longitude: 11.263162 }
  },
  {
    industry: 'Maritime Shipping',
    city: 'Genoa',
    address: 'Via Galeazzo Alessi, 1, 16128 Genova GE, Italy',
    location: { latitude: 44.4029039, longitude: 8.9384841 }
  },
  {
    industry: 'Tourism Hospitality',
    city: 'Venice',
    address: "Via degli Arditi, 35, 30013 Ca' Ballarin VE, Italy",
    location: { latitude: 45.4737973, longitude: 12.5279771 }
  },
  {
    industry: 'Food Processing',
    city: 'Bologna',
    address: 'Via Francesco Zanardi, 54, 40131 Bologna BO, Italy',
    location: { latitude: 44.5136966, longitude: 11.3258413 }
  },
  {
    industry: 'Ceramics Production',
    city: 'Faenza',
    address: 'Piazza 2 Giugno, 7/palazzo Muky, 48018 Faenza RA, Italy',
    location: { latitude: 44.2887423, longitude: 11.8772864 }
  }
]

Install node.js dependency

npm install axios

Run it

node get-address.js

Result enter image description here