Uploading and using data to a geoprocessing service

15486
28
12-19-2012 07:32 PM
TomSchuller
Occasional Contributor III
Hy,
I'm trying to build a simple geoprocessing service which should buffer an uploaded shapefile.


This is the scenario:
  1) uploading shapefile to geoprocessing service
  2) buffering the shapefile with a given parameter
  3) returning the buffered shapefile
 
The buffering is not the problem, but the handling of the upload and reusing it in the model or python part.
As far as I read, I should have to use a Datafile parameter for upload, but I'm always getting this error while running it:
ExecuteError: Failed to execute. Parameters are not valid. ERROR 000735: input: Value is required Failed to execute


Can anybody help me to use uploads on geoprocessing services?


Thanks,
Tom
0 Kudos
28 Replies
meriyalootka
New Contributor III
Hy
I have a model for uploading datafile, every thing is ok and user's zip file extract correctly. But I can not use extracted shapefile. Is there any py code for using feature class in a scratchfolder? Can I have this part of your model?



The datafile parameter input for the geoprocessing widget in the Viewer for Flex does not allow the user to upload a file to the server from their local machine.  It only accepts paths that begin with http:// or https://

I was under the impression that this was going to be fixed at the 3.1 release, but the widget still won't accept a local file.

This thread indicates all the pieces are available if you have the capability of creating your own custom widget:

http://forums.arcgis.com/threads/64114-Uploads-capability-on-Geoprocessor

There are many of us that cannot create custom widgets.  If ESRI will update the geoprocessing widget so it will take a local file,  I, and many other users will be very happy!

http://forums.arcgis.com/threads/54441-Uploading-to-SDE-from-Flex-Viewer
http://forums.arcgis.com/threads/41089-datafile-input-broken-in-Geoprocessing-widget
http://forums.arcgis.com/threads/36560-Geoprocessing-widget-has-me-stumped
http://forums.arcgis.com/threads/60510-Geoprocessing-with-File-Input
0 Kudos
KevinHibma
Esri Regular Contributor
The following code (10.1 + only) walks through a directory and finds FeatureClasses (this example assumes a folder of "zip" files).
It then copies each one into a fGDB. You can change what it does, but the logic in it should start you off.
You'll notice that a directory is created inside the scratchFolder (the variable "ZipFolder") - this is where the uploaded ZipFile is extracted to. Then the code looks in there.

import arcpy
import zipfile
import os

inFile = arcpy.GetParameterAsText(0) 

# Create a folder in the scratch directory to extract zip to
zipFolder = os.path.join(arcpy.env.scratchFolder, "zipContents")
os.mkdir(zipFolder)


# Extract the zip contents
zip2Extract = zipfile.ZipFile(inFile, 'r')
zip2Extract.extractall(zipFolder)
zip2Extract.close()

# Create a folder in the scratch directory to hold the fgdb which will be downloaded
fgdbFolder = os.path.join(arcpy.env.scratchFolder, "fgdbOutput")
os.mkdir(fgdbFolder)

# Work through all the FeatureClasses inside the extracted zip folder
for dirpath, dirnames, filenames in arcpy.da.Walk(zipFolder, datatype="FeatureClass"):
    
    for filename in filenames:
        
        # You could replace the code below here with your own code to do what you want....
        arcpy.AddMessage("Copying: {}".format(filename))
        
        # Strip .shp from the filename when merging shapefiles
        if filename.endswith("shp"):
            outFilename = filename[:-4]
        else:
            outFilename = filename

        # Copy each featureclass into the output.gdb
        arcpy.CopyFeatures_management(os.path.join(dirpath, filename),
                                      os.path.join(fgdbFolder, "output.gdb", outFilename))
0 Kudos
meriyalootka
New Contributor III
The following code (10.1 + only) walks through a directory and finds FeatureClasses (this example assumes a folder of "zip" files).
It then copies each one into a fGDB. You can change what it does, but the logic in it should start you off.
You'll notice that a directory is created inside the scratchFolder (the variable "ZipFolder") - this is where the uploaded ZipFile is extracted to. Then the code looks in there.

import arcpy
import zipfile
import os

inFile = arcpy.GetParameterAsText(0) 

# Create a folder in the scratch directory to extract zip to
zipFolder = os.path.join(arcpy.env.scratchFolder, "zipContents")
os.mkdir(zipFolder)


# Extract the zip contents
zip2Extract = zipfile.ZipFile(inFile, 'r')
zip2Extract.extractall(zipFolder)
zip2Extract.close()

# Create a folder in the scratch directory to hold the fgdb which will be downloaded
fgdbFolder = os.path.join(arcpy.env.scratchFolder, "fgdbOutput")
os.mkdir(fgdbFolder)

# Work through all the FeatureClasses inside the extracted zip folder
for dirpath, dirnames, filenames in arcpy.da.Walk(zipFolder, datatype="FeatureClass"):
    
    for filename in filenames:
        
        # You could replace the code below here with your own code to do what you want....
        arcpy.AddMessage("Copying: {}".format(filename))
        
        # Strip .shp from the filename when merging shapefiles
        if filename.endswith("shp"):
            outFilename = filename[:-4]
        else:
            outFilename = filename

        # Copy each featureclass into the output.gdb
        arcpy.CopyFeatures_management(os.path.join(dirpath, filename),
                                      os.path.join(fgdbFolder, "output.gdb", outFilename))



Hy
Many thanks
Please help me about input and output parameters.
0 Kudos
meriyalootka
New Contributor III
The following code (10.1 + only) walks through a directory and finds FeatureClasses (this example assumes a folder of "zip" files).
It then copies each one into a fGDB. You can change what it does, but the logic in it should start you off.
You'll notice that a directory is created inside the scratchFolder (the variable "ZipFolder") - this is where the uploaded ZipFile is extracted to. Then the code looks in there.

import arcpy
import zipfile
import os

inFile = arcpy.GetParameterAsText(0) 

# Create a folder in the scratch directory to extract zip to
zipFolder = os.path.join(arcpy.env.scratchFolder, "zipContents")
os.mkdir(zipFolder)


# Extract the zip contents
zip2Extract = zipfile.ZipFile(inFile, 'r')
zip2Extract.extractall(zipFolder)
zip2Extract.close()

# Create a folder in the scratch directory to hold the fgdb which will be downloaded
fgdbFolder = os.path.join(arcpy.env.scratchFolder, "fgdbOutput")
os.mkdir(fgdbFolder)

# Work through all the FeatureClasses inside the extracted zip folder
for dirpath, dirnames, filenames in arcpy.da.Walk(zipFolder, datatype="FeatureClass"):
    
    for filename in filenames:
        
        # You could replace the code below here with your own code to do what you want....
        arcpy.AddMessage("Copying: {}".format(filename))
        
        # Strip .shp from the filename when merging shapefiles
        if filename.endswith("shp"):
            outFilename = filename[:-4]
        else:
            outFilename = filename

        # Copy each featureclass into the output.gdb
        arcpy.CopyFeatures_management(os.path.join(dirpath, filename),
                                      os.path.join(fgdbFolder, "output.gdb", outFilename))



I need a model for extracting a zip file (contain shape file) to a feature class. I need to use from extracted shape file in my model. please help me more.
0 Kudos
meriyalootka
New Contributor III
I need a model for extracting a zip file (contain shape file) to a feature class. I need to use from extracted shape file in my model. please help me more.
0 Kudos
KevinHibma
Esri Regular Contributor
You have to use a script tool. Theres no tool in the toolbox (to put into modelbuilder) to extract a zipfile.
You can wrap the above script as a script tool and put that into a model if it serves your purpose.
0 Kudos
meriyalootka
New Contributor III
You have to use a script tool. Theres no tool in the toolbox (to put into modelbuilder) to extract a zipfile.
You can wrap the above script as a script tool and put that into a model if it serves your purpose.


I,m trying more than 1 month, but result is very bad. I tested last script, but can not use it!!
Last script return this error:
IndentationError: unexpected indent YourScript.py, line 1)
Failed to execute (Your script)

please give me a model if you can.
This is my email:meriyaloot@gmail.com

Regards
0 Kudos
meriyalootka
New Contributor III
You have to use a script tool. Theres no tool in the toolbox (to put into modelbuilder) to extract a zipfile.
You can wrap the above script as a script tool and put that into a model if it serves your purpose.


Hi esri
I,m a bad luck user. There isn't no one to solve my problem:mad:
0 Kudos
meriyalootka
New Contributor III
You have to use a script tool. Theres no tool in the toolbox (to put into modelbuilder) to extract a zipfile. 
You can wrap the above script as a script tool and put that into a model if it serves your purpose.


Hi
The script return this error:


Traceback (most recent call last):
File "C:\Users\administrator\Documents\importzip.py", line 22, in <module>
for dirpath, dirnames, filenames in arcpy.da.Walk(zipFolder, datatype="FeatureClass"):
AttributeError: 'module' object has no attribute 'Walk'

What is wrong in my setting?
0 Kudos
KevinHibma
Esri Regular Contributor
My apologies, the da.Walk command was added at 10.1 Service Pack 1. Based on the message I'm guessing you have 10.1 final (no service packs). If you're able to install the service pack it'll fix that.
0 Kudos