Example #1
0
##os.environ['RESTAPI_USE_ARCPY'] = '0'
from env import test_data_folder, delete_shapefile
##from restapi import open_source
import restapi
import random

def create_random_coordinates(xmin=-89000000, xmax=-9000000, ymin=4000000, ymax=4200000):
    return {
        "x": random.randint(xmin, xmax),
        "y": random.randint(ymin, ymax)
    }

url = 'https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Hazards_Uptown_Charlotte/FeatureServer/0'

# create FeatureLayer
hazards = restapi.FeatureLayer(url)

# QUERY EXAMPLES

# query all features, to fetch all regardless of `maxRecordCount` 
# use `exceed_limit=true` keyword arg
##fs = hazards.query()
##print('All Hazards Count: {}'.format(fs.count))
##
### query features that are "High" Priority
##high_priority = hazards.query(where="Priority = 'High'")
##print('High Priority Hazards count: {}'.format(high_priority.count))
##
### download features - choosing a geodatbase output will bring over domain 
### info (when you have access to arcpy), whereas a shapefile output will 
### just bring over the domain values
Example #2
0
import os
os.environ['RESTAPI_USE_ARCPY'] = 'FALSE'

# now import restapi
import json
import restapi

# download Allegheny County Covid19 data
url = "https://services1.arcgis.com/vdNDkVykv9vEWFX4/ArcGIS/rest/services/Spatial_Join_408/FeatureServer/0"
county_layer = restapi.FeatureLayer(url)

# export to shapefile
shp = "/Users/sihanmao/Documents/Projects/sihan_covid/covid19_pgh/map/county_covid19.shp"
county_layer.export_layer(shp, outSR=4326)

# convert to geojson for mapbox
os.system("ogr2ogr -f 'GeoJSON' county_covid19.geojson county_covid19.shp")

# delete crs for mapbox upload

with open(
        '/Users/sihanmao/Documents/Projects/sihan_covid/covid19_pgh/map/county_covid19.geojson'
) as f:
    data = json.load(f)

data["crs"] = ""

with open(
        '/Users/sihanmao/Documents/Projects/sihan_covid/covid19_pgh/map/county_covid19.geojson',
        'w') as f:
    json.dump(data, f)
Example #3
0
import restapi
import random
import requests


def create_random_coordinates(xmin=-89000000,
                              xmax=-9000000,
                              ymin=4000000,
                              ymax=4200000):
    return {"x": random.randint(xmin, xmax), "y": random.randint(ymin, ymax)}


url = 'https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Hazards_Uptown_Charlotte/FeatureServer/0'

# create FeatureLayer
hazards = restapi.FeatureLayer(url)

# QUERY EXAMPLES

# query all features, to fetch all regardless of `maxRecordCount`
# use `exceed_limit=true` keyword arg
fs = hazards.query()
print('All Hazards Count: {}'.format(fs.count))

# test with a different session object (all requests will have a "Test-Header" passed)
session = requests.Session()
session.headers = {'Test-Header': 'hello-world!'}
client = restapi.RequestClient(session)
hazards2 = restapi.FeatureLayer(url, client=client)
fs2 = hazards2.query()
print('All Hazards Count from other client: {}'.format(fs2.count))
Example #4
0
# export layer to a shapefile
cities.export_layer(shp, where=where)

# if there is an existing feature set, you can also export that directly
# restapi.exportFeatureSet(featureSet, shp)

# export a kmz
kmz = shp.replace('.shp', '.kmz')
cities.export_kmz(kmz, where=where)

# select layer by location
universities_url = 'https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Colleges_and_Universities/FeatureServer/0'

# get feature layer
universities = restapi.FeatureLayer(universities_url)
print('universities: ', repr(universities))

# form geometry (do not have to cast to restapi.Geometry, this will happen under the hood automatically)
geometry = restapi.Geometry({
    "spatialReference": {
        "latestWkid": 3857,
        "wkid": 102100
    },
    "rings": [[[-10423340.4579098, 5654465.8453829475],
               [-10324889.565478457, 5654465.8453829475],
               [-10324889.565478457, 5584449.527473665],
               [-10423340.4579098, 5584449.527473665],
               [-10423340.4579098, 5654465.8453829475]]]
})