Skip to main content

GeoJSON shapes

This page is for the practitioner who wants to manage the SAND stored-shape library and use those shapes in searches. All snippets below assume svi is an authenticated SVIClient.

Before this example​

Names and ids are deployment-specific. Before you adapt a snippet, use Discover Your Environment to understand the SAND configuration in your environment, especially:

  • the group label you want to organize shapes under ("<your-group>")
  • the entity type your search runs against ("<your-entity-type>")

List stored shapes​

Use svi.admin.shapes.list() to enumerate stored shapes. The SAND filter grammar supports only contains matching on the name and group properties; the SDK renders those for you. The match is case-sensitive, and the value is matched as given, spaces and punctuation included.

shapes = svi.admin.shapes.list(group="<your-group>", name_contains="Col")
for shape in shapes:
print(shape.id, shape.name, shape.group)

Why this layer matters:

  • The SDK pages for you and hands back one list.
  • The rows are bound Shape records, not raw payloads.
  • A Shape drops into a search filter without you copying its id.

Create a new shape​

Use svi.admin.shapes.create(...) to add a reusable shape. The geometry value is any GeoJSON geometry or geometry collection.

colorado = svi.admin.shapes.create(
name="Colorado",
group="<your-group>",
geometry={
"type": "Polygon",
"coordinates": [
[
[-109.04, 37.00],
[-102.05, 36.99],
[-102.07, 40.99],
[-109.04, 40.99],
[-109.04, 37.00],
]
],
},
)
print(colorado.id)

To load areas from a GeoJSON file without writing Python, use the CLI:

sasvi shape upload districts.geojson --group "<your-group>" --yes

It stores one shape per feature. See Use the sasvi CLI.

A Shape returned by the service can be passed straight into a geographic-shape filter using its .as_filter() shortcut.

shape = svi.admin.shapes.get(shape_id=colorado.id)
results = svi.search.types("<your-entity-type>").filter(shape.as_filter("within")).execute()

You can also pass a Shape to the DSL builder explicitly:

from sasvi.search import F

results = (
svi.search
.types("<your-entity-type>")
.filter(F.geo_shape(stored=shape, relation="within"))
.execute()
)

Supported relations are "within", "contains", "intersects", and "disjoint".

Update a shape with optimistic concurrency​

svi.admin.shapes.get(...) captures the server's ETag on the returned Shape. Passing the shape back to update(...) attaches the matching If-Match header so two writers cannot silently overwrite each other.

shape = svi.admin.shapes.get(shape_id=colorado.id)
print(shape.version) # ETag captured on get()
shape.raw.name = "Colorado (verified)"
updated = svi.admin.shapes.update(shape)
print(updated.name)

If another writer changed the shape since you fetched it, update(...) raises AdminConflictError, which is an AdminOperationError and a ConflictError. Refresh and retry.

The Shape returned by update(...) or create(...) carries the ETag the server sent back, so updated.version is the new version and a chained update(updated) sends it without a re-read. A Shape built from a plain mapping has no captured ETag; update(...) then reads the current one first. When the server sends no ETag at all, update(...) raises AdminOperationError before sending anything, because SAND requires If-Match on this route.

Delete shapes​

svi.admin.shapes.delete(shape_id=colorado.id)

svi.admin.shapes.delete_all() wipes the entire stored-shape library; use it with care.

See also​