-
Notifications
You must be signed in to change notification settings - Fork 1
ArcGIS Online Data
ArcGIS datasets can be queried just by the data, or by using spatial features and spatial queries. The bad news is that the API system is NOT well documented, and not very intuitive; it can be difficult because there are some formalities that are not obvious about which parameters are required. It usually takes a lot of frustrating trial-and-error to write an API call correctly.
Here is a fairly simple non-spatial example with 6 parameters:
const url = `//services.arcgis.com/fLeGjb7u4uXqeF9q/ArcGIS/rest/services/RTT_SUMMARY/FeatureServer/0/query`;
const theWhereClause = "(((ADDRESS_LOW >= " + address_low + " AND ADDRESS_LOW <= " + addressCeil + ")"
+ " OR (ADDRESS_LOW >= " + address_floor + " AND ADDRESS_LOW <= " + addressCeil + " AND ADDRESS_HIGH >= " + address_remainder + " ))"
+ " AND STREET_NAME = '" + geocode.street_name
+ "' AND STREET_SUFFIX = '" + geocode.street_suffix
+ "' AND (MOD(ADDRESS_LOW,2) = MOD( " + address_low + ",2))";
const params = {
where: theWhereClause,
outFields: "DOCUMENT_ID, DISPLAY_DATE, DOCUMENT_TYPE, GRANTORS, GRANTEES, UNIT_NUM, MATCHED_REGMAP, REG_MAP_ID",
returnDistinctValues: 'true',
returnGeometry: 'false',
f: 'json',
sqlFormat: 'standard',
}
const response = await axios(url, { params });
You can also write spatial queries with ArcGIS Online, and the syntax is quite different from the way Carto does it. For instance, if you have a polygon dataset representing parcels (property areas), and you want to know which one you are inside of, you would use your geometry.coordinates and an "esriSpatialRelWithin" query:
let ESRILayer = parcelLayer === 'pwd' ? 'PWD_PARCELS' : 'DOR_Parcel';
let params = {
'where': '1=1',
'outSR': 4326,
'f': 'geojson',
'outFields': '*',
'returnGeometry': true,
'geometry': `{ "x": ${lng}, "y": ${lat}, "spatialReference":{ "wkid":4326 }}`,
'geometryType': 'esriGeometryPoint',
'spatialRel': 'esriSpatialRelWithin',
};
const response = await axios(`https://services.arcgis.com/fLeGjb7u4uXqeF9q/ArcGIS/rest/services/${ESRILayer}/FeatureServer/0/query`, { params });
One of the issues that you trip up on a lot is that even if you do not need to include a WHERE clause, you still HAVE TO add one like "1=1", or the call will break.
Knowing that you needed the strings "spatialRel" and "esriSpatialRelWithin" in your query is really difficult. The ArcGIS REST APIs documentation is hard to navigate, and even when you find the right page like the Query (Feature Service) page, it's hard to follow. The trick is, you can use a dataset's service page for help writing a query. Go to a service page such as
https://services.arcgis.com/fLeGjb7u4uXqeF9q/ArcGIS/rest/services/RTT_SUMMARY/FeatureServer/0
Then click "query" at the bottom:
It will take you to a form that looks like this:
Fill out the fields on that form as you think you need to. Many of them are dropdowns, helping you put in correct values. Like for "Spatial Relationship" you can use a dropdown to select the word "Within".
The only 2 that are absolutely REQUIRED are "Where" (you can put 1=1) and "Out Fields" (you can put *).
Then at the bottom, set the Format to JSON, and click Query (GET)
This will open a new tab and the url in that tab will be the query that you created with the form, and the content will be the queried data:
You can copy that url out and use it. It will have filled out things like "...&spatialRel=esriSpatialRelWithin&..." since you chose "Within" from the dropdown. Only the fields you filled out will have values in the query in the url, but the parameters will be there for every field on the form, with blank values if you did not fill them out. So as you transfer this query into your js, you can leave out fields that weren't used.