Skip to main content

rgcosm simple reverse geocoding library from converted osm(.pbf) GIS data by converter in this lib

Project description

RGCosm - Reverse Geocode for OpenStreetmap

Upload Python Package

A Python library for offline reverse geocoding from osm(.pbf) GIS converted to sqlite3 data - based on code from rgcoms scripts

Install by:

pip install rgcoms

or from source by:

git clone https://github.com/BlackCatDevel0per/rgcosm
cd rgcoms
pip install build
python -m build

Dependencies

  1. osmium

CLI

See cli commands by:

python rgcosm -h

output:

usage: rgcosm [-h] [-ci CINPUT] [-co COUTPUT] [-ai ADD_INDEXES] [-db DATABASE]
                   [-ltln LAT_LON] [-lat LATITUDE] [-lon LONGITUDE] [-st SEARCH_TAGS]
                   [-mtc MIN_TAGS_COUNT] [-rd RETRIEVE_DEGREE] [-rt ROUND_TO]

rgcosm cli

optional arguments:
  -h, --help            show this help message and exit
  -ci CINPUT, --cinput CINPUT
                        Path to input pbf file
  -co COUTPUT, --coutput COUTPUT
                        Path to output db file
  -ai ADD_INDEXES, --add_indexes ADD_INDEXES
                        Add indexes for faster search default: True
  -db DATABASE, --database DATABASE
                        Path to db file
  -ltln LAT_LON, --lat_lon LAT_LON
                        latitude with longitude separated by space
  -lat LATITUDE, --latitude LATITUDE
                        latitude
  -lon LONGITUDE, --longitude LONGITUDE
                        longitude
  -st SEARCH_TAGS, --search_tags SEARCH_TAGS
                        tags to search, default: `addr:`
  -mtc MIN_TAGS_COUNT, --min_tags_count MIN_TAGS_COUNT
                        Minimal tags count (for `-st/--search_tags`) to filter result,
                        default: 1
  -rd RETRIEVE_DEGREE, --retrieve_degree RETRIEVE_DEGREE
                        Retrieve addresses within a +/- x degree range of the original
                        coordinates, default: 0.001
  -rt ROUND_TO, --round_to ROUND_TO
                        Round degree to n decimals after dot, default: 8

First convert downloaded osm(.pbf) files from:

https://download.geofabrik.de/

Then use cli to create the database (speedupped by using db in ram & dump in to disk):

python rgcosm -ci some-place.osm.pbf -co some-place.db

The output file can be x7-13 (for maldives file ~12.74 times) times larger then the source file, for example maldives file size is 2.7 mb, and after conversion size increased to 34.4 mb (time: ~14 sec.) with added indexes and 20.1 mb without (time: ~13 sec.).

You can disable adding indexes by -ai=no or --add_indexes=no arg.

Adding indexes speedups searching time up to 70 times.

Usage

from rgcosm import get_address
db_path = 'maldives-latest.db'
coordinates = (6.5506617, 72.9530232)
addr = get_address(db_path, coordinates)
print(addr)

result:

[{'id': 9508099415, 'lat': 6.5506617, 'lon': 72.9530232, 'tags': {'addr:block_number': '26', 'generator:method': 'combustion', 'generator:output:electricity': '200 kV', 'generator:source': 'diesel', 'name': 'Vaikaradhoo Fenaka Power Plant 3', 'operator': 'Fenaka Corporation Limited Vaikaradhoo', 'power': 'generator'}}]

or with multiple coordinates:

from rgcosm import get_address
db_path = 'maldives-latest.db'
coordinates = [(6.5506617, 72.9530232), (4.172474, 73.5083067), (4.1718557, 73.5154427)]
addr = get_address(db_path, coordinates)
print(addr)

result:

[{'id': 9508099415, 'lat': 6.5506617, 'lon': 72.9530232, 'tags': {'addr:block_number': '26', 'generator:method': 'combustion', 'generator:output:electricity': '200 kV', 'generator:source': 'diesel', 'name': 'Vaikaradhoo Fenaka Power Plant 3', 'operator': 'Fenaka Corporation Limited Vaikaradhoo', 'power': 'generator'}}, {'id': 2521220337, 'lat': 4.172474, 'lon': 73.5083067, 'tags': {'addr:city': "Male'", 'addr:housename': 'Ma.Seventy Flower', 'addr:street': 'Iskandharu Magu', 'amenity': 'cafe', 'cuisine': 'coffee_shop', 'internet_access': 'yes', 'name': "Chili's Café"}}, {'id': 7987147424, 'lat': 4.1718557, 'lon': 73.5154427, 'tags': {'addr:city': "Male'", 'addr:housenumber': 'H.Hostside', 'addr:postcode': '20053', 'addr:street': 'Irudheymaa Hingun', 'clothes': 'women;wedding;men;suits;fashion;children', 'contact:facebook': 'https://m.facebook.com/Aiccet/', 'currency:EUR': 'yes', 'currency:GBP': 'yes', 'currency:USD': 'yes', 'name': 'Aiccet', 'opening_hours': '24/7', 'operator': 'Aiccet', 'payment:american_express': 'yes', 'payment:cash': 'yes', 'payment:credit_cards': 'yes', 'payment:mastercard': 'yes', 'payment:visa': 'yes', 'payment:visa_debit': 'yes', 'phone': '+960 7997323', 'shop': 'clothes'}}]

Advanced (for keep connection to db):

from rgcosm import RGeocoder
db_path = 'maldives-latest.db'
geo = RGeocoder(db_path)
coordinates = [(4.1758869, 73.5094013), (-0.6699146, 73.1228688), (5.159217, 73.1312907)]
addrs = geo.locate(coordinates, 'addr:', 1)
print(addrs)

result:

[{'id': 10300135473, 'lat': 4.1758869, 'lon': 73.5094013, 'tags': {'addr:city': "Male'", 'email': 'silverlinehotelsupplier@gmail.com', 'name': 'Silverline Hotel Supplies', 'office': 'company', 'phone': '732-9577', 'website': 'http://www.silverlineenterprise.com/'}}, {'id': 9446166886, 'lat': -0.6699146, 'lon': 73.1228688, 'tags': {'addr:city': 'Addu City', 'addr:housenumber': 'Mushkuraanaage', 'addr:postcode': '19030', 'addr:street': 'Dhandivara Maga'}}, {'id': 8439302155, 'lat': 5.159217, 'lon': 73.1312907, 'tags': {'addr:city': 'Dharavandhoo', 'addr:postcode': '06060', 'amenity': 'courthouse', 'name': 'Dharavandhoo Magistrate Court', 'opening_hours': 'Sa-Th 08:00-14:00', 'operator': 'Government of Maldives'}}]

In plans:

  • db serializing with lz4 compression & etc.
  • Add more formats for addresses
  • Add caching results
  • More speedup conversion & less memory usage
  • Add some features from other similar libs
  • More documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rgcosm-0.1.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

rgcosm-0.1.2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file rgcosm-0.1.2.tar.gz.

File metadata

  • Download URL: rgcosm-0.1.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for rgcosm-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ea43f6f1730ef6857eef0ea8f83c321da7f208e1806a70b22bbb56b715d811eb
MD5 0c995ae3df75e0cc5bb2e294aa9c2161
BLAKE2b-256 a8cb6e1f79a09d1e80bf73e48f746ef60307cc55aadd8af4c777494c7a902543

See more details on using hashes here.

File details

Details for the file rgcosm-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: rgcosm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for rgcosm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8e984fee828a74c1d05dd16183b1b4da95de3f1f1a12f573a811af1fac370686
MD5 e676ab69336c93bebddac0b1fbb529a0
BLAKE2b-256 6472a701e14f9f048082aa5e3d5f3fb340220739816427bdfc67d312d284e71b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page