1

My code is generating coordinates (Lat, Lon) of 1000 houses. Now I want to reverse these 1000 coordinates to get full address. The code is:

import pprint
from arcgis.geocoding import reverse_geocode
Houses = [0]*(1000)
Houses[0], Houses[1] = (143.5689855, -38.328956999999996), (143.5692555, -38.328993)
for i in range(2, 1000):
    latitude_diff = Houses[i-1][0] - Houses[i-2][0]
    longitude_diff= Houses[i-1][1] - Houses[i-2][1]
    temp = (Houses[i-1][0]+latitude_diff, Houses[i-1][1]+longitude_diff)

    Houses[i] = temp
pprint.pprint(Houses)

I tried reverse_code from ArcGIS but it only converts single coordinate like below:

results = reverse_geocode([143.5689855, -38.328956999999996])

How can I reverse the coordinates of 1000 houses into full addresses?

2

Your error was probably similar to this one

Traceback (most recent call last):
  File "D:/test.py", line 24, in <module>
    results = reverse_geocode(pt)
  File "C:\...\_functions.py", line 1054, in reverse_geocode
    geocoder = arcgis.env.active_gis._tools.geocoders[0]
AttributeError: 'NoneType' object has no attribute '_tools'

It is most likely because of the credentials that have to be specified gis = GIS("http://www.arcgis.com", "username", "password").

Otherwise, I would doubt that ArcGIS libraries could be deployed without a licence.

So, the final code will look as following (including comment)

from arcgis.geocoding import reverse_geocode
from arcgis.geometry import Geometry
from arcgis.gis import GIS
import csv

gis = GIS("http://www.arcgis.com", "***", "***")

houses = []
houses.append((143.5689855, -38.328956999999996))
houses.append((143.5692555, -38.328993000000000))

lat_diff = houses[1][0] - houses[0][0]
lon_diff = houses[1][1] - houses[0][1]

i = 1
while i <= 5:
    houses.append((houses[i][0] + lat_diff, houses[i][1] + lon_diff))
    i = i + 1

result = []

for x, y in houses:
    pt = Geometry({
        "x": float(x),
        "y": float(y),
        "spatialReference": {
            "wkid": 4326
        }
    })
    try:
        result.append(reverse_geocode(pt))
    except:
        pass

i = 0
result_to_csv = []

for item in result:
    i += 1
    result_item = {
        'id': i,
        'address': item['address']['LongLabel'],
        'lat': round(item['location']['x'],6),
        'lon': round(item['location']['y'],6)
    }
    result_to_csv.append(result_item)

keys = result_to_csv[0].keys()

with open('output.csv', 'w', encoding='utf8', newline='') as output_file:
    dict_writer = csv.DictWriter(output_file, keys, delimiter=';', quoting=csv.QUOTE_NONE, lineterminator='\r')
    dict_writer.writeheader()
    dict_writer.writerows(result_to_csv)

The output csv-file

output

when 1.000 of houses have to be geocoded then adjust this part while i < 999.

P.S. I am not an expert in programming, so I am looking forward to any improvements/suggestion for this script.

| improve this answer | |
  • @Thank you for great results. Can we save the above results to .CSV?. For instance: The first column give us address and 2nd column give us latitude,Latitude. – Case Msee Mar 25 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.