Vector Example

The vector service exposes a highly scalable backend for ingesting and searching over millions of features. This example creates a new product, adds features, searches for features and then deletes the product.

The API reference for these methods and classes is at descarteslabs.vectors.

from descarteslabs.vectors import Feature, FeatureCollection, properties as p
import uuid

Let’s make a test FeatureCollection use FeatureCollection.create.

id_suffix = str(uuid.uuid4())
fc = FeatureCollection.create(
    product_id="my_test_product" + id_suffix,
    title="My Test Product",
    description="This product was created using an example file.",
)

print(fc)

Out:

FeatureCollection({
  u'description': u'This product was created using an example file.',
  'id': u'd4ef22d5a6969cb61147ec8ea3e060cdf33e1a49:...product8b85d625-7a9d-47d8-93b6-075779892ee4',
  u'name': None,
  u'owners': [u'user:d4ef22d5a6969cb61147ec8ea3e060cdf33e1a49', u'org:descarteslabs'],
  u'readers': [],
  u'title': u'My Test Product',
  u'writers': []
})

Let’s load this existing FeatureCollection from an id.

id_ = fc.id

fc = FeatureCollection(id_)
print(fc)

Out:

FeatureCollection({
  u'description': u'This product was created using an example file.',
  'id': u'd4ef22d5a6969cb61147ec8ea3e060cdf33e1a49:...product8b85d625-7a9d-47d8-93b6-075779892ee4',
  u'name': None,
  u'owners': [u'user:d4ef22d5a6969cb61147ec8ea3e060cdf33e1a49', u'org:descarteslabs'],
  u'readers': [],
  u'title': u'My Test Product',
  u'writers': []
})

Let’s create a Feature for this product and add it using FeatureCollection.add.

feature = Feature(
    geometry={
        "type": "Polygon",
        "coordinates": [
            [
                [-105.86975097656249, 36.94550173495345],
                [-104.930419921875, 36.94550173495345],
                [-104.930419921875, 37.70120736474139],
                [-105.86975097656249, 37.70120736474139],
                [-105.86975097656249, 36.94550173495345],
            ]
        ],
    },
    properties={"foo": "bar"},
)

fc.add([feature])

Search for features in this product intersecting an aoi using FeatureCollection.filter and FeatureCollection.features.

aoi = {
    "type": "Polygon",
    "coordinates": [
        [
            [-105.194091796875, 36.88181755936464],
            [-104.765625, 36.88181755936464],
            [-104.765625, 37.13404537126446],
            [-105.194091796875, 37.13404537126446],
            [-105.194091796875, 36.88181755936464],
        ]
    ],
}

fc = fc.filter(geometry=aoi)
print(list(fc.features()))  # this returns an iterator

Out:

[Feature({
  'geometry': {
    'coordinates': (((-105.86975097656249, 36.94550173495345), (-104.930419921875, 36.94550173495345), (-104.930419921875, 37.70120736474139), (-105.86975097656249, 37.70120736474139), (-105.86975097656249, 36.94550173495345)),),
    'type': 'Polygon'
  },
  'id': u'30161802c75b2c50af5c44da80624734e649734da...e6554c4a45aad0b295f87ed536_d49f7878f75f47f3',
  'properties': {
    u'foo': u'bar'
  },
  'type': 'Feature'
})]

Search for Feature in this FeatureCollection intersecting the aoi where foo=bar.

print(list(fc.filter(properties=(p.foo == "bar")).features()))

Out:

[Feature({
  'geometry': {
    'coordinates': (((-105.86975097656249, 36.94550173495345), (-104.930419921875, 36.94550173495345), (-104.930419921875, 37.70120736474139), (-105.86975097656249, 37.70120736474139), (-105.86975097656249, 36.94550173495345)),),
    'type': 'Polygon'
  },
  'id': u'30161802c75b2c50af5c44da80624734e649734da...e6554c4a45aad0b295f87ed536_d49f7878f75f47f3',
  'properties': {
    u'foo': u'bar'
  },
  'type': 'Feature'
})]

Search for Feature where property foo=foo. There should be no results.

print(list(fc.filter(properties=(p.foo == "foo")).features()))

Out:

[]

And cleanup

fc.delete()

Total running time of the script: ( 0 minutes 7.465 seconds)

Gallery generated by Sphinx-Gallery