22from openlocationcode import openlocationcode as olc
33
44# https://github.com/google/open-location-code/blob/master/python/openlocationcode_test.py
5+ # https://grid.plus.codes/
56
67
78def check_spatial_reference (feature_class ):
9+ """
10+ Check if the feature class is using the Geographic Reference System WGS84 (Lat / Long). The wkid should be 4326.
11+ An exception will be raised if that's not the case.
12+ :param feature_class: The feature class that will be used to generate the Open Location Code.
13+ :return:
14+ """
815 sr = arcpy .Describe (feature_class ).spatialReference
916 if sr .factoryCode != 4326 :
1017 raise ValueError ('The input feature class must be in WHS84 (WKID 4326)' )
1118
1219
13- def validate_plus_code_length (code ):
20+ def validate_plus_code_length (code_length ):
21+ """
22+ Validate if the Plus Code Length is valid. That does not account for the character '+' added by the API. The
23+ function will raise an exception if the number of character used is not valid.
24+ :param code_length: The number of characters used to encode the Open Location Code.
25+ :return: None
26+ """
1427
1528 # Those are the number of digit required from the specifications.
1629 valid_codes_length = [
@@ -23,9 +36,9 @@ def validate_plus_code_length(code):
2336 12 # Level 6
2437 ]
2538
26- if code not in valid_codes_length :
39+ if code_length not in valid_codes_length :
2740 raise ValueError ('Valid Plus Code must be one of the following value: {}' .format (
28- ', ' .join ([str (code ) for code in valid_codes_length ])
41+ ', ' .join ([str (code_length ) for code_length in valid_codes_length ])
2942 ))
3043
3144
@@ -56,7 +69,7 @@ def generate_plus_code(feature_class, plus_code_field, code_length):
5669
5770 :param feature_class: The input feature class. The EPSG must be 4326
5871 :param plus_code_field: The field that will contain the plus codes.
59- :param code_length: The maximum length for the plus code .
72+ :param code_length: The number of characters used to encode the Open Location Code .
6073 :return:
6174 """
6275
@@ -69,16 +82,14 @@ def generate_plus_code(feature_class, plus_code_field, code_length):
6982 # Validate the field is long enough to accomodate the code.
7083 validate_code_field_length (feature_class , plus_code_field , code_length )
7184
72- count = 0
85+ # Get the number of features. This will be used to set a progress bar.
86+ count_features = int (arcpy .management .GetCount (feature_class )[0 ])
87+ arcpy .SetProgressor ('step' , 'Computing Plus Codes ...' , 0 , count_features , 1 )
88+
7389 with arcpy .da .UpdateCursor (feature_class , ['SHAPE@XY' , plus_code_field , 'OID@' ]) as input_cursor :
7490 for input_row in input_cursor :
7591
7692 oid = input_row [- 1 ]
77- count += 1
78-
79- if count % 100000 == 0 :
80- arcpy .AddMessage ('Processed {} rows so far ...' .format (count ))
81-
8293 try :
8394 longitude = input_row [0 ][0 ]
8495 latitude = input_row [0 ][1 ]
@@ -89,6 +100,8 @@ def generate_plus_code(feature_class, plus_code_field, code_length):
89100 arcpy .AddWarning (
90101 'Something went wrong with feature OID: {} - Error Message: {}' .format (oid , ex .message )
91102 )
103+ finally :
104+ arcpy .SetProgressorPosition ()
92105
93106
94107if __name__ == '__main__' :
0 commit comments