3

I'd like to query the geometry from a Mapserver: https://map.dfg.ca.gov/arcgis/rest/services/Project_BIOS_Public

The ulimtate goal is to query all the geometry for each layer and perform an intersect against a reference shapefile. I want to determine which layers intersect with a given watershed.

This is my first time working with the ArcGIS REST service. I'm a little confused on how I can extract only the geometry and the layer name... for the purposes of this exercise I just want a list of which layers intersect with a watershed I have as a shapefile.

I've written the following code (just specific URL for example)

import requests
import json
import urllib
from collections import defaultdict

queryURL = 'https://map.dfg.ca.gov/arcgis/rest/services/Project_BIOS_Public/q_BIOS_Public_pointslines00/MapServer/0/query'
params = urllib.parse.urlencode({'f': 'geojson', 'geometry':'Envelope', 'returnGeometry': 'true', 'OutFields':'*'}).encode("utf-8")


#requests query
req = urllib.request.Request(queryURL,params)

#opens the response
response = urllib.request.urlopen(req)

#loads response as json
jsonResult = json.load(response)

jsonResult

This returns the geometry but also all of the fields, and not the layer name. How do I get just the geometry and the layer name?

4
  • You've got multiple questions embedded in this question. If the REST endpoint doesn't accept a fields/columns list parameter, then the query will return all fields. I doubt you could ever make it return layer name, unless that's one of the fields. Returning geometry from rasters is unlikely, though extracting the envelope, from which 5-point polygons can be extracted should be simple enough.
    – Vince
    Commented Nov 10, 2019 at 18:29
  • You will need to convert your shapefile to geojson to send that query - there is only 708 features in that rest end point - Vince is correct envelope the query will be the quickest way.
    – Mapperz
    Commented Nov 10, 2019 at 19:49
  • Sorry for having more than one question in here. I can send the geometry of the shapefile to the rest service query to determine if they intersect? I see that now. Regarding the rasters, I need to extract that as an envelope? I'm having trouble constructing the url :map.dfg.ca.gov/arcgis/rest/services/Project_BIOS_Public/…
    – jacmosh
    Commented Nov 10, 2019 at 20:01
  • I've edited out the too broad raster query question; you can ask that after you do some more research.
    – Vince
    Commented Nov 11, 2019 at 3:29

1 Answer 1

1

It's not quite the same, but the JS API interface documentation on outFields indicates that the REST API OutFields parameter is a column list. You've provided:

params = urllib.parse.urlencode(
            {'f': 'geojson', 
             'geometry':'Envelope', 
             'returnGeometry': 'true', 
             'OutFields':'*'}
         ).encode("utf-8")

where instead you could have provided:

params = urllib.parse.urlencode(
            {'f': 'geojson', 
             'geometry':'Envelope', 
             'returnGeometry': 'true', 
             'OutFields':'shape'} # <<--- Or whatever the geometry column name is
         ).encode("utf-8")

which would have limited the return result to geometry.

If the data source was a view which hard-coded the source name in the result, as in:

CREATE VIEW mylayer_v AS 
    SELECT objectid,field1,...fieldN,'mylayer'::varchar(32) as layername 
    FROM   mylayer

then you could query the layer published off mylayer_v with

params = urllib.parse.urlencode(
            {'f': 'geojson', 
             'geometry':'Envelope', 
             'returnGeometry': 'true', 
             'OutFields':['shape','layername']} #  (might be 'shape,layername' -- haven't checked)
         ).encode("utf-8")

It would probably be much easier to populate the layer source on the client side than to hard-code the source into the database (since it's rarely a 1:1 relationship). It would also reduce the I/O and database overhead to avoid this approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.