Solved! Go to Solution.
import arcpy, os, zipfile infile = arcpy.GetParameterAsText(0) outpath, outfileext = os.path.splitext(infile) filename = outpath.split('\\')[-1] try: # unzip file fireZip = zipfile.ZipFile(infile, 'r') fireZip.extractall(outpath) fireZip.close() shpPath = outpath + "\\" + filename + ".shp" arcpy.AddMessage("Finished unzipping file.") # Local variables: WildFire_Table_Target = "Database Connections\\SQL_DB(TEST Server).sde\\ArcSDE.dbo.WILDFIREPERIM" # Create FieldMappings object and load the target dataset fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(WildFire_Table_Target) inputfields = [field.name for field in arcpy.ListFields(shpPath) if not field.required] for inputfield in inputfields: # Iterate through each FieldMap in the FieldMappings # for i in range(fieldmappings.fieldCount): fieldmap = fieldmappings.getFieldMap(i) #arcpy.AddMessage(fieldmap.getInputFieldName(0)) # If the field name from the target dataset matches to a validated input field name # if fieldmap.getInputFieldName(0) == inputfield.replace("", ""): # Add the input field to the FieldMap and replace the old FieldMap with the new # fieldmap.addInputField(shpPath, inputfield) fieldmappings.replaceFieldMap(i, fieldmap) break # Process: Append arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings) except Exception as e: print e.message arcpy.AddError(e.message)
Create FieldMappings object and load the target dataset # fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(WildFire_Table_Target) inputfields = [field.name for field in arcpy.ListFields(shpPath) if not field.required] for inputfield in inputfields: # Iterate through each FieldMap in the FieldMappings # for i in range(fieldmappings.fieldCount): fieldmap = fieldmappings.getFieldMap(i) #arcpy.AddMessage(fieldmap.getInputFieldName(0)) # If the field name from the target dataset matches to a validated input field name # if fieldmap.getInputFieldName(0) == inputfield.replace("", ""): # Add the input field to the FieldMap and replace the old FieldMap with the new # fieldmap.addInputField(shpPath, inputfield) fieldmappings.replaceFieldMap(i, fieldmap) break
Geoprocessor geoprocessorTask = new Geoprocessor("http://mygisserver/arcgis/rest/services/GPTools/WildfireUpload/GPServer/ZipImportTest"); geoprocessorTask.ExecuteCompleted += GeoprocessorTask_ExecuteCompleted; geoprocessorTask.Failed += GeoprocessorTask_Failed; List<GPParameter> parameters = new List<GPParameter>(); parameters.Add(new GPItemID("Input_File", "iec5a4db4-33c9-40b9-ab82-cd27ecef279f")); //http://gis/arcgis/rest/services/GPTools/WildfireUpload/GPServer/uploads/iec5a4db4-33c9-40b9-ab82-cd27ecef279f old file link geoprocessorTask.ExecuteAsync(parameters);
import arcpy, os, zipfile infile = arcpy.GetParameterAsText(0) outpath, outfileext = os.path.splitext(infile) filename = outpath.split('\\')[-1] try: # unzip file fireZip = zipfile.ZipFile(infile, 'r') fireZip.extractall(outpath) fireZip.close() shpPath = outpath + "\\" + filename + ".shp" arcpy.AddMessage("Finished unzipping file.") # Local variables: WildFire_Table_Target = "Database Connections\\SQL_DB(TEST Server).sde\\ArcSDE.dbo.WILDFIREPERIM" # Create FieldMappings object and load the target dataset fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(WildFire_Table_Target) inputfields = [field.name for field in arcpy.ListFields(shpPath) if not field.required] for inputfield in inputfields: # Iterate through each FieldMap in the FieldMappings # for i in range(fieldmappings.fieldCount): fieldmap = fieldmappings.getFieldMap(i) #arcpy.AddMessage(fieldmap.getInputFieldName(0)) # If the field name from the target dataset matches to a validated input field name # if fieldmap.getInputFieldName(0) == inputfield.replace("", ""): # Add the input field to the FieldMap and replace the old FieldMap with the new # fieldmap.addInputField(shpPath, inputfield) fieldmappings.replaceFieldMap(i, fieldmap) break # Process: Append arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings) except Exception as e: print e.message arcpy.AddError(e.message)
{"itemID":"i80dfa12f-52ed-4841-94ff-37f9c3f5dd6f"}
Sorry I got ahead of you there.
Because I needed to perform the field mapping on the fly i switched from using a model to using a python script. The input parameter type is going to be "File" in any case. In the script below, "infile = arcpy.GetParameterAsText(0)" is going to get the input file as a File type parameter. Then you can use the file as you please.
You will see when you run this in desktop you have a file browser that you can use to browse to a file of your choice. Once you get that to run succesfully you can publish as a service. With the service you will need to get the itemID of the uploaded file and use that as an input parameter like itemID:"i83fa38d5-69e8-40c0-bd9c-30beb643e522".
import arcpy, os, zipfile infile = arcpy.GetParameterAsText(0) outpath, outfileext = os.path.splitext(infile) filename = outpath.split('\\')[-1] try: # unzip file fireZip = zipfile.ZipFile(infile, 'r') fireZip.extractall(outpath) fireZip.close() shpPath = outpath + "\\" + filename + ".shp" arcpy.AddMessage("Finished unzipping file.") # Local variables: WildFire_Table_Target = "Database Connections\\SQL_DB(TEST Server).sde\\ArcSDE.dbo.WILDFIREPERIM" # Create FieldMappings object and load the target dataset fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(WildFire_Table_Target) inputfields = [field.name for field in arcpy.ListFields(shpPath) if not field.required] for inputfield in inputfields: # Iterate through each FieldMap in the FieldMappings # for i in range(fieldmappings.fieldCount): fieldmap = fieldmappings.getFieldMap(i) #arcpy.AddMessage(fieldmap.getInputFieldName(0)) # If the field name from the target dataset matches to a validated input field name # if fieldmap.getInputFieldName(0) == inputfield.replace("", ""): # Add the input field to the FieldMap and replace the old FieldMap with the new # fieldmap.addInputField(shpPath, inputfield) fieldmappings.replaceFieldMap(i, fieldmap) break # Process: Append arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings) except Exception as e: print e.message arcpy.AddError(e.message)
Hi
I tested your code, but it return this error:
IndentationError: unindent does not match any outer indentation level
How should I set script input and output parameters?