2

My code is generating coordinates (Lat, Lon) of 1000 houses. Now I want to reverse these 1000 coordinates to get the 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_geocode() from ArcGIS API for Python 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?

Taras
35.8k5 gold badges77 silver badges151 bronze badges
asked Mar 24, 2020 at 13:28
0

1 Answer 1

3

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 license.

So, the final code will look as follows (including comments)

import csv
from arcgis.gis import GIS
from arcgis.geometry import Geometry
from arcgis.geocoding import reverse_geocode
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.

answered Mar 24, 2020 at 17:57
1
  • @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. Commented Mar 25, 2020 at 1:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.