geo
Patentcity geo
general principle: address (str) -> structured geo data (dict) 3 flavors: libpostal, HERE, GMAPS
libpostal (parser)¶
Libpostal https://github.com/openvenues/libpostal Docker libpostal https://github.com/johnlonganecker/libpostal-rest-docker REST api https://github.com/johnlonganecker/libpostal-rest
Note: i) if set up on GCP, you need to set up firewall rules to authorize access from the requesting
machine ii) get external IP of GCP compute engine
https://console.cloud.google.com/networking/addresses/list?project=
HERE Batch (geocoding)¶
Guide: developer.here.com/documentation/batch-geocoder/dev_guide/topics/request-constructing.html API ref: https://developer.here.com/documentation/batch-geocoder/dev_guide/topics/endpoints.html
Gmaps (geocoding)¶
API ref
- https://developers.google.com/maps/documentation/geocoding/start
- https://developers.google.com/maps/documentation/geocoding/overview
add_geoc_data(file, geoc_file, source=None, max_workers=5, verbose=False)
¶
Add geoc data from geoc_file
to file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
file path |
required |
geoc_file |
str
|
geoc file path (geocoding output, csv) |
required |
source |
str
|
geocoding service (in ["HERE", "GMAPS", "MANUAL"]) |
None
|
max_workers |
int
|
max number of workers |
5
|
verbose |
bool
|
verbosity |
False
|
Usage:
patentcity geo add entrel_uspatentxx.jsonl geoc_uspatentxx.here.csv --source HERE
Source code in patentcity/geo.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
add_geoc_disamb(disamb_file, index_geoc_file, flavor='GMAPS', inDelim='|')
¶
Return a list of recId|geoc(target) from a list of recid|target.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
disamb_file |
str
|
disambiguation data file path |
required |
index_geoc_file |
str
|
index geocoding file path |
required |
flavor |
str
|
flavor of |
'GMAPS'
|
inDelim |
str
|
inner delimiter |
'|'
|
Usage:
patentcity geo add.disamb ${DISAMBFILE} ${GEOCINDEX} --flavor ${FLAVOR}
Info
Use before patentcity geo add
Source code in patentcity/geo.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
|
add_statisticalareas(file, statisticalareas_path, verbose=False)
¶
Return file
with statistical areas to stdout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
file path |
required |
statisticalareas_path |
str
|
satistical area files path (wildcard allowed) |
required |
verbose |
bool
|
verbosity |
False
|
Usage:
patentcity geo add.statisticalareas geoc_gbpatentxx.here.csv "assets/statisticalareas_*.csv"
Source code in patentcity/geo.py
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 |
|
get_geoc_data_gmaps(file, api_key, region, language='en', max_workers=5, inDelim='|', skip_header=True)
¶
Geocode addresses in file
using GMAPS
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
file path |
required |
api_key |
str
|
api key |
required |
region |
str
|
region code, specified as a ccTLD (“top-level domain”) two-character value (e.g. de, fr, uk, us, etc). |
required |
language |
str
|
the language in which to return results |
'en'
|
max_workers |
int
|
max number of workers |
5
|
inDelim |
str
|
inner delimiter |
'|'
|
skip_header |
bool
|
whether to ski header or not |
True
|
Usage:
patentcity geo gmaps.get loc_uspatentxx.txt $APIKEY us
Info
Source code in patentcity/geo.py
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
|
get_geoc_data_here(request_id, api_key, output_dir=None, unzip=True)
¶
Download and save HERE geocoded data to output_dir
/request_id
.zip
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id |
str
|
HERE job request ID (returned by `here.post) |
required |
api_key |
str
|
HERE api key |
required |
output_dir |
str
|
saving directory |
None
|
unzip |
bool
|
whether to unzip the output |
True
|
Usage:
patentcity geo here.get $REQUESTID $APIKEY --output-dir <your-dir>
Info
Source code in patentcity/geo.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
get_geoc_status_here(request_id, api_key, freq=5, verbose=False)
¶
Check status of job request_id
every freq
seconds
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id |
str
|
HERE job request ID (returned by `here.post) |
required |
api_key |
str
|
HERE api key |
required |
freq |
int
|
interval between 2 consecutive status updates |
5
|
verbose |
bool
|
verbosity |
False
|
Usage:
patentcity geo here.status $REQUESTID $APIKEY
Source code in patentcity/geo.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
get_parsed_loc_libpostal(path, api_reference, max_workers=10, debug=False)
¶
Send data in path
to libpostal service (hosted at api_reference
)
and return parsed loc json blobs to stdout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
data path (wildcard allowed) |
required |
api_reference |
str
|
reference of service host "ip:port" |
required |
max_workers |
int
|
max number of workers |
10
|
debug |
bool
|
verbosity degree |
False
|
Usage:
patentcity geo libpostal.get <your-addresses.txt> <ip:port>
Source code in patentcity/geo.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
harmonize_geoc_data_gmaps(file, inDelim='|', out_format='csv', header=True)
¶
Harmonize Gmaps response with HERE Geocoding API responses (csv)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
file path |
required |
inDelim |
str
|
inner delimiter |
'|'
|
out_format |
str
|
format of the output (in ["csv", "jsonl"]) |
'csv'
|
header |
bool
|
whether to add a header (if |
True
|
Usage:
patentcity geo gmaps.harmonize geoc_uspatentxx.gmaps.jsonl
Source code in patentcity/geo.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
|
post_geoc_data_here(file, api_key, countryfocus, outCols=None, inDelim='|', outDelim=',', locationattributes='addressDetails', language='en-EN', includeinputfields=False, verbose=False)
¶
Post file
to HERE batch geocoding API
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
file path. File is expected to be formatted as follows recId|searchText |
required |
api_key |
str
|
HERE api key |
required |
countryfocus |
str
|
iso3 country code (e.g. deu, fra, gbr, usa, etc), see Format input |
required |
outCols |
str
|
None
|
|
inDelim |
str
|
'|'
|
|
outDelim |
str
|
','
|
|
locationattributes |
str
|
'addressDetails'
|
|
language |
str
|
output language, see Request parameters |
'en-EN'
|
includeinputfields |
bool
|
False
|
|
verbose |
bool
|
verbosity |
False
|
Usage:
patentcity geo here.post loc_uspatentxx.txt $APIKEY usa
Source code in patentcity/geo.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
prep_geoc_data(file, inDelim='|')
¶
Return patentees' loc data formatted for geocoding to stdout (recId|searchText).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
file path |
required |
inDelim |
str
|
inner delimiter used by HERE |
'|'
|
Usage:
patentcity geo prep entrel_uspatent01.jsonl
#Sort and deduplicate addresses before batch geocoding
sort -u loc_uspatent01.txt
Source code in patentcity/geo.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|