SpatialQuery Syntax
This page documents the syntax of SpatialQuery. It assumes that the reader has already read (or at least knows about) the SpatialQuery overview.
Table of Contents
The Basics
SpatialQuery lets you retrieve spatial data from a URL. SpatialQuery arguments are encoded in the URL's query string.
URL Escaping
SpatialQuery URLs contain special characters that should be URL-escaped. See the guide to URL escaping.
Constructing URLs
Base Path
The base path for SpatialQuery requests is:
http://spatial.mapfluence.com/spatialquery.<format>
where <format> is either json or geojson and specifies the reponse format.
from (required)
The from clause specifies what geometry datasets to select from. Geometry datasets are specified as "<dataset.geometryset>". The data catalog provides a full list of available geometry datasets.
from=us_census00.blkgrp_geom where=intersects([417817,417822])
It is also possible to select from multiple geometry datasets:
from=us_census00.place_geom;us_census00.county_geom
select=name
where=contains_pct('POLYGON((-87.218 33.104,-87.513 32.964,-87.764 32.765,-87.218 33.104))',0.5)
select
By default, SpatialQuery returns data for all of the built-in fields of features. You can choose to add additional attribute date to the response and/or limit the built-in fields that will be returned.
Limiting the field list can be a powerful tool for improving performance. Specifically, it is good not to select the geometry field if you don't need it. To limit what built-in fields are returned, simply put the fields that you do want in select:
select=id;name from=us_census00.county_geom
To select attribute data, put the fully qualified field name (as "dataset.table.field", which you can look up in the data catalog) in select:
select=uselect_pres.state_data.rep_pty_votes from=us_census00.state_geom where=name:'Florida'
It is also possible to select attribute data from specific period(s):
select=obesity.data@2001.pct_obese from=us_census00.state_geom
And multiple periods:
select=obesity.data@[2001,2005].pct_obese from=us_census00.state_geom
Or to select multiple fields from the same attribute table in one statement:
select=obesity.data@[2001,2005].[pct_obese,pct_owght] from=us_census00.state_geom
If you do not specify a select clause, or specify only attribute (non built-in) fields, Mapfluence will return all of the built-in fields.
aggregate
Use the aggregate argument instead of select to get a data summary rather than a list of features. Because of this, you cannot use both a select clause and an aggregate clause in the same request.
The syntax is:
aggregate=<OPERATOR>(<operand>)
| operator | meaning |
|---|---|
| MIN | find the minimum of all the operands |
| MAX | find the maximum of all the operands |
| SUM | find the sum of all the operands |
| AVG | find the average of all the operands |
| COUNT | count results (operand must be '*') |
For example, if you want to find out how many counties in the US are named "Washington", you would use a query like this:
aggregate=COUNT(*) from=us_census00.county_geom where=name:'Washington'
To get the minimum percent of people not in the labor force in US Census places:
aggregate=MIN(us_census00.stats.pct_nilf) from=us_census00.place_geom
Note that you while you can use as many aggregators as you want on any combination of the built-in fields and one attribute dataset, you can't aggregate from multiple attribute datasets in the same request. This means you can do
aggregate=COUNT(*);SUM(area);AVG(uselect_pres.county_data.uselect_pres.county_data.prt_mccain_votes) from=us_census00.county_geom where=name:'Washington'
but the following will give you an error:
aggregate=AVG(uselect_pres.county_data.uselect_pres.county_data.prt_mccain_votes);AVG(unemplmnt.data.unemplevel) from=us_census00.county_geom where=name:'Washington'
where
The where clause filters results by data and geometry criteria.
Geometry filters
Geometry filters have the following syntax:
where=<operator>(<geometry>[,<value>])
Available operators:
| operator | meaning | example use |
|---|---|---|
| intersects | does the geometry in the record intersect the geometry specified? | show me roads which go at least part-way into Illinois |
| contains_pct | does the geometry specified lie at least percentage% within the geometry in the record? | show me roads that are at least 50% in Illinois |
| pct_within | does at least percentage% of the geometry in the record lie within geometry specified? | show me the states where at least 10% of Route 66 lies within that state |
The intersects operator does not take a value parameter, but the contains_pct and pct_within operators take a floating point number between 0 and 1 that gives the percent contained or within needed to satisfy the expression. In addition to filtering the results, contains_pct and pct_within will append the appropriate percentage to the results if percentage is lower than 1. By specifying 1 for percentage, you can get the same behavior as a GIS-enabled database will give you for CONTAINS or WITHIN.
The geometry parameter can be feature ID(s) or a geometry in WKT format. You can also use Mapfluence's RANGE extension to WKT.
For example, the following URL gives all the zip code numbers (and the geometry ids) that are within a 10 mile radius of 37.776702N 122.398939W (the location of UMI's San Francisco headquarters):
where=intersects('RANGE(10mi POINT(-122.398939 37.776702))')
select=name
from=us_census00.zcta_geom
Data filters
SpatialQuery permits data filters on both built-in fields and attribute dataset fields. Data filters have the following syntax:
where=<field>__<operator>:<value>
field can be either a built-in field ("population") or a fully-qualified attribute dataset field ("us_census00.stats.av_vhhsz")
See data filters for a list of available operators and for details on how to format value.
Combining filters
You can use multiple filters in the where clause, separated by '+'. Filters are combined with 'AND', so features must match every criteria. There is no support for 'OR', although you can make multiple requests and combine the results.
This URL returns the name and population of all Census places within 10 miles of a point that have an unemployment rate of at least 9:
where=unemplmnt.data.unemplevel__gte:9+intersects('RANGE(10mi POINT(-122.4 37.8))')
select=geometry;name
from=us_census00.place_geom
order_by
The order_by clause is just what you'd expect if you are familiar with SQL: it tells which field to use as a sort key for ordering the results. (In other words, the record where the sort key is numerically lowest will be first and the record where it is highest will be last.)
For example:
order_by=healthcvrg.uninsured.cnt_u_1864
select=name
from=us_census00.county_geom
where=intersects('RANGE(10mi POINT(-122.436033 37.759749))')
will retrieve counties ordered by the count of uninsured people. You can order the results in descending order by prepending '-' to the field:
select=name
order_by=-healthcvrg.uninsured.cnt_u_1864
from=us_census00.county_geom
where=intersects('RANGE(10mi POINT(-122.436033 37.759749))')
between
Each UMI dataset has a named period where it is valid, and you can specify the time period that you are interested in. (This prevents you from getting results from both the 1990 census and the 2000 census.)
The period name is specific to a table. For example, the US Census runs every ten years, so the US Census dataset has a period named "2000" that is defined to run from 1 Jan 2000 to 31 Dec 2009. Check the Data Catalog in the Mapfluence lab for the definition of periods for that data set (look near the bottom of the dataset's Data Tables page).
The between clause lets you specify a date (really a date/time) or date range for the data you want to select. A date will get the period that applied on that date, and a date range will get data from whatever periods overlap that date range.
Dates have the following format, and date ranges are separated by '-':
YYYY[ MM[ DD[ HH[ MM[ SS]]]]]
between=2002 03 04-2006 select=obesity.data.pct_obese from=us_census00.state_geom
limit
The limit clause acts much like a LIMIT clause in SQL: the value specifies how many records will be returned. For example, this query is limited to three records:
limit=3 from=us_census00.zcta_geom
Limit, if provided, must be between 1 and 100. The default is 100.
offset
Offset works like its SQL equivalent. By performing multiple requests with different offsets, you can retrieve all results from a query that contains more than 100 features.
ashtml
The ashtml argument is useful for debugging. It formats the JSON or GeoJSON output as html such that it can be easily read in a web browser.
ashtml=1 from=us_census00.zcta_geom limit=3