colab Open in SageMaker Studio Lab Open in Planetary Computer

Creating A Bounding Box From Queries

Tutorial created by **David Montero Loaiza**: GitHub | Twitter

Let’s start!

If required, please uncomment:

[1]:
#!pip install eemont
#!pip install geemap

Import the required packages.

[2]:
import ee, eemont, geemap

Authenticate and Initialize Earth Engine and geemap.

[3]:
Map = geemap.Map()

Creating a Bounding Box (ee.Geometry)

A query is a string representing a place that is geocoded in order to get its Bounding Box coordinates.

[4]:
query = 'Bogotá, Colombia'

In eemont, a Bounding Box can be constructed from a query using the geopy package.

NOTE: Neither eemont nor geopy are geocoding services.

An ee.Geometry.BBox can be constructed using the ee.Geometry.BBoxFromQuery constructor (extended through eemont):

[5]:
BBoxFromQuery = ee.Geometry.BBoxFromQuery(query,user_agent = 'eemont-tutorial-011')

The user_agent argument must be specified: This is a string describing the name of the app that is using a geocoding service (you can use here your GEE username).

Let’s visualize our Bounding Box (color blue):

[6]:
Map.addLayer(BBoxFromQuery,{'color':'blue'},'Nominatim')
Map.centerObject(BBoxFromQuery,10)
Map

By default, the geocoding service used is nominatim (Open Street Maps). But it can be modified using the geocoder parameter (let’s use the arcgis geocoding service):

[7]:
BBoxFromQuery = ee.Geometry.BBoxFromQuery(query,geocoder = 'arcgis',user_agent = 'eemont-tutorial-011')

Let’s visualize our Bounding Box (color red):

[8]:
Map.addLayer(BBoxFromQuery,{'color':'red'},'Arcgis')
Map.centerObject(BBoxFromQuery,10)
Map

As you can see, different geocoding services may give different results.

Creating a Bounding Box with Properties (ee.Feature)

A feature can also be created using the ee.Feature.BBoxFromQuery constructor (extended through eemont):

[9]:
featureFromQuery = ee.Feature.BBoxFromQuery(query,user_agent = 'eemont-tutorial-011')

Let’s explore the feature:

[10]:
featureFromQuery.getInfo()
[10]:
{'type': 'Feature',
 'geometry': {'geodesic': False,
  'type': 'Polygon',
  'coordinates': [[[-74.22351370000001, 4.4711754],
    [-74.01025770000001, 4.4711754],
    [-74.01025770000001, 4.8331695],
    [-74.22351370000001, 4.8331695],
    [-74.22351370000001, 4.4711754]]]},
 'properties': {'boundingbox': ['4.4711754',
   '4.8331695',
   '-74.2235137',
   '-74.0102577'],
  'class': 'boundary',
  'display_name': 'Bogotá, Bogotá Distrito Capital - Municipio, 11001, Colombia',
  'icon': 'https://nominatim.openstreetmap.org/ui/mapicons//poi_boundary_administrative.p.20.png',
  'importance': 0.9031743429157826,
  'lat': '4.6533326',
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
  'lon': '-74.083652',
  'osm_id': 7426387,
  'osm_type': 'relation',
  'place_id': 283502085,
  'type': 'administrative'}}

The raw properties of the place obtained from the geocoding service are converted into the feature properties. Let’s check the properties from the arcgis geocoder:

[11]:
featureFromQuery = ee.Feature.BBoxFromQuery(query,geocoder = 'arcgis',user_agent = 'eemont-tutorial-011')
featureFromQuery.getInfo()
[11]:
{'type': 'Feature',
 'geometry': {'geodesic': False,
  'type': 'Polygon',
  'coordinates': [[[-74.46040999999997, 4.223960000000053],
    [-73.67840999999993, 4.223960000000053],
    [-73.67840999999993, 5.005960000000053],
    [-74.46040999999997, 5.005960000000053],
    [-74.46040999999997, 4.223960000000053]]]},
 'properties': {'address': 'Bogota, Bogota, D.C.',
  'attributes': {},
  'extent': {'xmax': -73.67840999999994,
   'xmin': -74.46040999999995,
   'ymax': 5.005960000000053,
   'ymin': 4.223960000000053},
  'location': {'x': -74.06940999999995, 'y': 4.614960000000053},
  'score': 100}}

Different properties are obtained using different geocoders.