Skip to main content

A Python package for working with world cities, countries, regions database

Project description

AigeoDB

A Python package for working with world cities, countries, regions database. This package provides easy access to a comprehensive database of world locations.

About

Developed by Unrealos Inc. - We create innovative SaaS and PaaS solutions powered by AI for business. Our expertise includes:

  • AI-powered business solutions
  • SaaS platforms
  • PaaS infrastructure
  • Custom enterprise software

Features

  • Easy-to-use interface for querying geographical data
  • Built-in database downloader and updater
  • Support for searching cities, countries, and regions
  • Geolocation features (nearby cities search)
  • SQLAlchemy models for all database entities
  • Django integration with custom model fields

Installation

Basic installation:

pip install aigeodb

With Django integration:

pip install aigeodb[django]

Core Usage

Basic Example

from aigeodb import DatabaseManager

# Initialize the database manager
db = DatabaseManager()

# Search for cities
cities = db.search_cities("Moscow", limit=5)
for city in cities:
    print(f"{city.name}, {city.country_code}")

# Get country information
country = db.get_country_info("US")
print(country.name, country.iso2)

# Find nearby cities
nearby = db.get_nearby_cities(40.7128, -74.0060, radius_km=100)
for city in nearby:
    print(f"{city.name}, {city.state_code}")

API Reference

# DatabaseManager methods
db = DatabaseManager()

# Search cities by name
cities = db.search_cities("Moscow", limit=5)

# Get country information
country = db.get_country_info("US")

# Calculate distance between two points
new_york = (40.7128, -74.0060)  # (latitude, longitude)
london = (51.5074, -0.1278)
distance = db.calculate_distance(new_york, london)
print(f"Distance between cities: {distance:.1f}km")

# Find nearby cities (simple usage)
cities = db.get_nearby_cities(
    latitude=40.7128, 
    longitude=-74.0060,
    radius_km=100,
    limit=10
)
for city in cities:
    print(f"{city.name}, {city.state_code}")

# Find nearby cities with distances
cities_with_distances = db.get_nearby_cities(
    latitude=40.7128,
    longitude=-74.0060,
    radius_km=100,
    limit=10,
    with_distance=True
)
for city, distance in cities_with_distances:
    print(f"{city.name}: {distance:.1f}km")

# Get cities by country
cities = db.get_cities_by_country("US")

# Get states/regions by country
states = db.get_states_by_country("US")

# Get database statistics
stats = db.get_statistics()

Distance Calculation

The package uses geopy for precise distance calculations using the geodesic formula. Coordinates are passed as tuples of (latitude, longitude).

Example distances:

# Some major city coordinates
new_york = (40.7128, -74.0060)
london = (51.5074, -0.1278)
paris = (48.8566, 2.3522)
tokyo = (35.6762, 139.6503)
seoul = (37.5665, 126.9780)

# Calculate distances
print(f"New York to London: {db.calculate_distance(new_york, london):.1f}km")  # ~5,570km
print(f"Paris to Tokyo: {db.calculate_distance(paris, tokyo):.1f}km")  # ~9,713km
print(f"Tokyo to Seoul: {db.calculate_distance(tokyo, seoul):.1f}km")  # ~1,160km

Database Content

The package includes:

  • Countries (250 records)
  • Regions (6 records)
  • Subregions (22 records)
  • States/Regions/Municipalities (5,038 records)
  • Cities/Towns/Districts (151,072 records)

Django Integration

AigeoDB provides Django model fields with Select2-powered autocomplete support for cities and countries.

Installation

pip install aigeodb[django]

Setup

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'django_select2',  # Required for autocomplete
    'aigeodb.django',  # Our fields and widgets
]

Using Fields

from django.db import models
from aigeodb.django.fields import CityField, CountryField

class Location(models.Model):
    city = CityField()
    country = CountryField()
    
    # Fields can be optional
    departure_city = CityField(null=True, blank=True)
    
    def __str__(self):
        city = self.city.get_data()
        country = self.country.get_data()
        return f"{city['name']}, {country['name']}"

Admin Integration

from django.contrib import admin

@admin.register(Location)
class LocationAdmin(admin.ModelAdmin):
    list_display = ('city_name', 'country_name')
    search_fields = ('city', 'country')
    
    def city_name(self, obj):
        data = obj.city.get_data()
        return f"{data['name']}, {data['country_code']}"
    
    def country_name(self, obj):
        data = obj.country.get_data()
        return f"{data['name']} ({data['iso2']})"

Caching (Recommended)

For better performance, configure caching in your settings.py:

# For development
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
    }
}

# For production (recommended)
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/1',
    }
}

Features

  • Select2-powered autocomplete for cities and countries
  • Automatic caching of search results and data
  • Support for optional (nullable) fields
  • Built-in data validation
  • Admin interface integration
  • Thread-safe database access

License

MIT License - see the LICENSE file for details.

Credits

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

aigeodb-0.1.3a3.tar.gz (13.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aigeodb-0.1.3a3-py2.py3-none-any.whl (13.7 MB view details)

Uploaded Python 2Python 3

File details

Details for the file aigeodb-0.1.3a3.tar.gz.

File metadata

  • Download URL: aigeodb-0.1.3a3.tar.gz
  • Upload date:
  • Size: 13.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for aigeodb-0.1.3a3.tar.gz
Algorithm Hash digest
SHA256 12ca30b42e8015d6895949e85f7c6724e94065266ff11b4894ca4ab9a6e01cb5
MD5 a4242ac1b0f1b0b66c8da1d488bd6850
BLAKE2b-256 875e74efb2dff224481addbca1f65fac4872d8b450a2427e30fa35f1c596ba9d

See more details on using hashes here.

File details

Details for the file aigeodb-0.1.3a3-py2.py3-none-any.whl.

File metadata

  • Download URL: aigeodb-0.1.3a3-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.7 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for aigeodb-0.1.3a3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6ed387bbd9e335269db7d9f7679ffe32c6538524b47c42be45e207abb526181e
MD5 38a1fe5aaf4e2682f9c5068fd13575f4
BLAKE2b-256 fed3e153af7ada28213e43c01fe289e4f578d17129d2abbcda33494a3dad5934

See more details on using hashes here.

Supported by

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