0% found this document useful (0 votes)
82 views2 pages

Script Python by Pak Cul

This Python script connects to an ArcGIS Server map service and downloads vector layer features to shapefiles. It handles requests in batches when there are more features than the server's maximum allowed in one request. The script gets all object IDs, divides them into groups at the maximum request limit, then queries and saves each group as a shapefile. Finally it appends the shapefiles into one layer for analysis in a GIS application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views2 pages

Script Python by Pak Cul

This Python script connects to an ArcGIS Server map service and downloads vector layer features to shapefiles. It handles requests in batches when there are more features than the server's maximum allowed in one request. The script gets all object IDs, divides them into groups at the maximum request limit, then queries and saves each group as a shapefile. Finally it appends the shapefiles into one layer for analysis in a GIS application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#Name: Export ArcGIS Server Map Service Layer to Shapefile with Iterate

#Author: Bryan McIntosh


#Description: Python script that connects to an ArcGIS Server Map Service and
downloads a single vector layer to shapefiles. If there are more features than AGS
max allowed, it will iterate to extract all features.

import urllib2,json,os,arcpy,itertools
ws = os.getcwd() + os.sep

#Set koneksi ke ArcGIS Server, map service, layer ID, dan jumlah maksimal request
ke server (1000 adalah default jika tidak diketahui).
#Contoh alamat koneksi ke geoportal kemenlhk adalah
http://geoportal.menlhk.go.id/arcgis/rest/services/KLHK/KHG/MapServer/0/query, maka
dibagi menjadi 3, tulisan query di belakang tidak disertakan, sehingga menjadi;

serviceURL = "http://geoportal.menlhk.go.id/arcgis/rest/services"
serviceMap = "/KLHK/BURN_AREA_2017/MapServer"
serviceLayerID = 0
serviceMaxRequest = 1000

#set nama file json dan shp output


dataOutputName = "Burn"

def defServiceGetIDs():
IDsRequest = serviceURL + serviceMap + "/" + str(serviceLayerID) + "/query?
where=1%3D1&text=&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR
=&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=&returnGeometry=true
&returnTrueCurves=false&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly
=true&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistic
s=&returnZ=false&returnM=false&gdbVersion=&returnDistinctValues=false&resultOffset=
&resultRecordCount=&f=pjson"
IDsResponse = urllib2.urlopen(IDsRequest)
IDsJSON = json.loads(IDsResponse.read())
IDsSorted = sorted(IDsJSON['objectIds'])
return IDsSorted

def defGroupList(n, iterable):


args = [iter(iterable)] * n
return ([e for e in t if e != None] for t in itertools.izip_longest(*args))

def defQueryExtractRequests(idMin, idMax):


myQuery = "&where=objectid+>%3D+" + idMin + "+and+objectid+<%3D+" + idMax
myParams = "query?
geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelIntersects&relationParam
=&outFields=*&returnGeometry=true&geometryPrecision=&outSR=&returnIdsOnly=false&ret
urnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&returnZ=false&returnM
=false&returnDistinctValues=false&returnTrueCurves=false&f=pjson"
myRequest = serviceURL + serviceMap + "/" + str(serviceLayerID) + "/" +
myParams + myQuery
response = urllib2.urlopen(myRequest)
myJSON = response.read()

# Write response to json text file


foo = open(dataOutputName + idMin + ".json", "w+")
foo.write(myJSON);
foo.close()

# Create Feature Class


arcpy.JSONToFeatures_conversion(dataOutputName + idMin + ".json", ws +
dataOutputName + idMin + ".shp")

#**MAIN**#
#Get all objectIDs (OIDs) for the layer (there is no server limit for this request)
AllObjectIDs = defServiceGetIDs()

#Divide the OIDs into chunks since there is a limit to map queries (assumed limit
stored in serviceMaxRequest variable)
ObjectID_Groups = list(defGroupList(serviceMaxRequest, AllObjectIDs))

#Membuat shapefile for masing-masing geojson


for ObjectID_Group in ObjectID_Groups:
idMin = str(ObjectID_Group[0])
idMax = str(ObjectID_Group[-1])
defQueryExtractRequests(idMin, idMax)

#Gabungkan shp hasil dengan perintah Append di aplikasi SIG

You might also like