Skip to main content
deleted 116 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

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

So, the final code will look as followingfollows (including commentcomments)

import csv
from arcgis.geocodinggis import reverse_geocodeGIS
from arcgis.geometry import Geometry
from arcgis.gisgeocoding import GISreverse_geocode
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 csvCSV-file

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.

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

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.

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

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

Tiny adjustments
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

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 you can use 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 <<= 105:
    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:
        results = result.append(reverse_geocode(pt))
    except:
        print(results)pass

i = 0
result_to_csv = except[]

for item in result:
    i += 1
  pass  result_item = {

So, I ended up with such result

{'address': {'Match_addr': '45 Stodart St, Colac, Victoria, 3250', 'LongLabel': '45 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '45 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '45', 'Address': '45 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5689855, 'y': -38.328956999999996, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '43 Stodart St, Colac, Victoria, 3250', 'LongLabel': '43 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '43 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '43', 'Address': '43 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5692555, 'y': -38.328993, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '41 Stodart St, Colac, Victoria, 3250', 'LongLabel': '41 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '41 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '41', 'Address': '41 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.56951650000002, 'y': -38.32902, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '39 Stodart St, Colac, Victoria, 3250', 'LongLabel': '39 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '39 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '39', 'Address': '39 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.56978650000002, 'y': -38.329046999999996, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '37 Stodart St, Colac, Victoria, 3250', 'LongLabel': '37 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '37 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '37', 'Address': '37 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5700205, 'y': -38.329074, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '35 Stodart St, Colac, Victoria, 3250', 'LongLabel': '35 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '35 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '35', 'Address': '35 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5702635, 'y': -38.329101, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}

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

Your code ended up with an error most likely because of the credentials that have to be specified gis = GIS("http://www.arcgis.com", "username", "password").

Your probable error was similar to this one

Traceback (most recent call last)    'id': i,
  File "D     'address':/test.py" item['address']['LongLabel'], 
 line 24, in <module>    'lat': round(item['location']['x'],6),
    results = reverse_geocode  'lon': round(ptitem['location']['y'],6)
  File "C:\. }
    result_to_csv.append(result_item)

keys = result_to_csv[0].\_functionskeys()

with open('output.py"csv', line'w', 1054encoding='utf8', innewline='') reverse_geocodeas output_file:
    geocoderdict_writer = arcgis.env.active_giscsv._toolsDictWriter(output_file, keys, delimiter=';', quoting=csv.geocoders[0]QUOTE_NONE, lineterminator='\r')
AttributeError: 'NoneType' object has nodict_writer.writeheader()
 attribute '_tools'  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.

I would doubt that you can use ArcGIS libraries without a licence.

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

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 < 10:
    houses.append((houses[i][0] + lat_diff, houses[i][1] + lon_diff))
    i = i + 1

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

So, I ended up with such result

{'address': {'Match_addr': '45 Stodart St, Colac, Victoria, 3250', 'LongLabel': '45 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '45 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '45', 'Address': '45 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5689855, 'y': -38.328956999999996, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '43 Stodart St, Colac, Victoria, 3250', 'LongLabel': '43 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '43 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '43', 'Address': '43 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5692555, 'y': -38.328993, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '41 Stodart St, Colac, Victoria, 3250', 'LongLabel': '41 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '41 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '41', 'Address': '41 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.56951650000002, 'y': -38.32902, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '39 Stodart St, Colac, Victoria, 3250', 'LongLabel': '39 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '39 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '39', 'Address': '39 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.56978650000002, 'y': -38.329046999999996, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '37 Stodart St, Colac, Victoria, 3250', 'LongLabel': '37 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '37 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '37', 'Address': '37 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5700205, 'y': -38.329074, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}
{'address': {'Match_addr': '35 Stodart St, Colac, Victoria, 3250', 'LongLabel': '35 Stodart St, Colac, Victoria, 3250, AUS', 'ShortLabel': '35 Stodart St', 'Addr_type': 'PointAddress', 'Type': '', 'PlaceName': '', 'AddNum': '35', 'Address': '35 Stodart St', 'Block': '', 'Sector': '', 'Neighborhood': 'Colac', 'District': '', 'City': 'Colac', 'MetroArea': '', 'Subregion': '', 'Region': 'Victoria', 'Territory': '', 'Postal': '3250', 'PostalExt': '', 'CountryCode': 'AUS'}, 'location': {'x': 143.5702635, 'y': -38.329101, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}

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

Your code ended up with an error most likely because of the credentials that have to be specified gis = GIS("http://www.arcgis.com", "username", "password").

Your probable error was 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'

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.

Tiny adjustments
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

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

Your code ended up with an error most likely because of the credentials that have to be specified gis = GIS("http://www.arcgis.com", "username", "password").

Your code ended up with an error most likely because of the credentials that have to be specified gis = GIS("http://www.arcgis.com", "username", "password").

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

Your code ended up with an error most likely because of the credentials that have to be specified gis = GIS("http://www.arcgis.com", "username", "password").

Tiny adjustments
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148
Loading
Tiny adjustments
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148
Loading
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148
Loading