Skip to main content

WKT generator

Project description

wkt

wkt makes it easy to grab Well-Known Text strings for countries, states, and cities around the world.

Here's how you can grab the polygon for New York State for example:

import wkt

wkt.us.states.new_york() # => "POLYGON((-79.7624 42.5142,-79.0672 42.7783..."

wkt is interoperable with many Pythonic geospatial tools like Shapely, GeoPandas, Sedona, and Dask!

Installation

Just run pip install wkt.

This library doesn't have any dependencies, so it's easy to install anywhere.

Shapely + wkt

Let's create a Shapely polygon with wkt:

import wkt
from shapely import from_wkt

alaska = from_wkt(wkt.us.states.alaska())

Check to make sure that a Shapely Polygon is created:

type(alaska) # => shapely.geometry.polygon.Polygon

Compute the area of the polygon:

alaska.area # => 353.4887780300002

GeoPandas + wkt

Create a GeoPandas DataFrame with wkt:

import geopandas as gpd
import pandas as pd

data = {
    "state": ["colorado", "new_mexico"],
    "geometry": [from_wkt(wkt.us.states.colorado()), from_wkt(wkt.us.states.new_mexico())]
}
df = pd.DataFrame(data)
gdf = gpd.GeoDataFrame(df, geometry="geometry")

Add a column with centroids:

gdf['centroid'] = gdf.geometry.centroid

Look at the results:

        state                     geometry                     centroid
0    colorado  POLYGON ((-109.0448 37.0004,  POINT (-105.54643 38.99855)
1  new_mexico  POLYGON ((-109.0448 36.9971,  POINT (-106.10366 34.42267)

Sedona + wkt

Read the Overture Maps Foundation places dataset:

places = sedona.table("wherobots_open_data.overture_maps_foundation.places_place")
places.createOrReplaceTempView("places")

Find all the barbecue restaurants in the state of Florida:

query = f"""
select * from places
where
    categories.primary = 'barbecue_restaurant' and
    ST_Contains(ST_GeomFromWKT('{wkt.us.states.florida()}'), geometry)
"""
res = sedona.sql(query)
res.count() # => 1386

Creating wkts

Use Overture Maps Foundation Divisions dataset to generate wkts.

divisions_df = sedona.table("wherobots_open_data.overture_maps_foundation.divisions_division_area")
divisions_df.createOrReplaceTempView("division_area")

To generate a wkt of a country use subtype, 'country':

country_iso = "US" # ISO code of the country

query = f"""
SELECT ST_AsEWKT(geometry) AS wkt
FROM division_area
WHERE subtype = 'country'
AND country = '{country_iso}'
"""

wkt = sedona.sql(query).collect()[0][0]

To generate a wkt of a state/region in a country use subtype, 'region':

country_iso = "US" # ISO code of the country
state_iso = "US-AZ" # ISO code of the state

query = f"""
SELECT ST_AsEWKT(geometry) AS wkt
FROM division_area
WHERE subtype = 'region'
AND country = '{country_iso}'
AND region = '{state_iso}'
"""

wkt = sedona.sql(query).collect()[0][0]

To generate a wkt of a city use subtype, 'locality':

Make sure to use the country and state filter when filtering by city_name. There may be more than one city with the same name.

country_iso = "US" # ISO code of the country
state_iso = "US-AZ" # ISO code of the state
city_name = 'Phoenix'

query = f"""
SELECT ST_AsEWKT(geometry) AS wkt
FROM division_area
WHERE subtype = 'locality'
AND country = '{country_iso}'
AND region = '{state_iso}'
AND names.primary = '{city_name}'
"""

wkt = sedona.sql(query).collect()[0][0]

Contributing

Feel free to submit a pull request with additional WKTs!

You can also create an issue to discuss ideas before writing any code.

You can also check issues with the "help wanted" tag for contribution ideas.

Developing

You can run the test suite with uv run pytest tests.

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

wkt-0.1.3.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

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

wkt-0.1.3-py3-none-any.whl (31.2 kB view details)

Uploaded Python 3

File details

Details for the file wkt-0.1.3.tar.gz.

File metadata

  • Download URL: wkt-0.1.3.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.3

File hashes

Hashes for wkt-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8d16c4fb2d4be2a8ee62f6fdbba765a5263e1db9893eefc10dab6adf7d7a851d
MD5 bb30c4027011c7c2ed06ac27c45bb338
BLAKE2b-256 1032af7a7316d4b8e1628802acb816fcffbbdd9e46e3a51d82a48813e9f426a7

See more details on using hashes here.

File details

Details for the file wkt-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: wkt-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.3

File hashes

Hashes for wkt-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f20718bf1d4725f9227d4301b6f45b2bf74bdb264346032271b35535477a747c
MD5 1db6211ca7fd10eacc3a4bab4e5d7f3c
BLAKE2b-256 6a83763e58245a86e159f230d76ed5c449db4001de501cb4f7ad02955f3af3aa

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