Select to view content in your preferred language

Zip File to GDB Service

4450
25
Jump to solution
12-14-2012 09:52 AM
BrianLeroux
Frequent Contributor
I am trying to create a service that allows a user to upload a zip file that contains a shape file, unzip it, and finally append to an existing GDB Feature Class. The fields in the shape file do not match the FC so I need to performa field mapping. However, since the input is a zip file I do not have the capabilty to set up the field mapping in the model. So when I run this it adds the elements of the shape file but all the attributes are blank because the missing mapping. Is there any way around this? [ATTACH=CONFIG]19948[/ATTACH]
0 Kudos
25 Replies
meriyalootka
Occasional Contributor
Just be sure to check your python code to make sure none of the lines are wrapping on you. Does it say what line the indentation error is on?

There is one input parameter scripted in this sample. infile = arcpy.GetParameterAsText(0). If you need more you can just take a similar appraoch.


The error is for Line 41.
0 Kudos
BrianLeroux
Frequent Contributor
The error is for Line 41.

I am not exactly sure what your line 41 would be. i would assume you changed the code as least a little bit so my line 41 is not the same as yours. If you can post your code and state the text of the line that is giving you issues I can see if I can identify the issue. When posting your code make sure it posts with the correct indents and line breaks or it could make it look like you have more problems that aren;t necesarily in your code.
0 Kudos
meriyalootka
Occasional Contributor
I am not exactly sure what your line 41 would be. i would assume you changed the code as least a little bit so my line 41 is not the same as yours. If you can post your code and state the text of the line that is giving you issues I can see if I can identify the issue. When posting your code make sure it posts with the correct indents and line breaks or it could make it look like you have more problems that aren;t necesarily in your code.


Hi

I guess the error related to this line:

arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings)


I did not change your code, I got it from your answer to TOM.

I wonder if you would mind to email your model (meriyaloot@gmail.com)

regards
0 Kudos
BrianLeroux
Frequent Contributor
Honestly it is probably just an issue with the indentation due to copying and pasting. I would just go though and make sure everything is indented properly. Using a program like notepad++ will make it easier to spot issues. When I copied my code below into NP++ you can see that line is indented too much as well as 2 other below it. Just delete the white space before that following 3 lines and tab them each over 1 time.

arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings)
print e.message
arcpy.AddError(e.message)
0 Kudos
meriyalootka
Occasional Contributor
Honestly it is probably just an issue with the indentation due to copying and pasting. I would just go though and make sure everything is indented properly. Using a program like notepad++ will make it easier to spot issues. When I copied my code below into NP++ you can see that line is indented too much as well as 2 other below it. Just delete the white space before that following 3 lines and tab them each over 1 time. 

arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings) 
print e.message 
arcpy.AddError(e.message)


I changed it with your solution and it return this error:
SyntaxError: invalid syntax


This is my new code:
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(ks204060).sde\\ArcSDE.dbo.gis"

# 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)
print e.message
arcpy.AddError(e.message)
0 Kudos
BrianLeroux
Frequent Contributor
You are going to have to post your code to this thread using code blocks. Without that I will not be able to properly view your code with the correct indenting.
0 Kudos
meriyalootka
Occasional Contributor
You are going to have to post your code to this thread using code blocks. Without that I will not be able to properly view your code with the correct indenting.


I attached pyton script. please help me.
0 Kudos
BrianLeroux
Frequent Contributor
I attached pyton script. please help me.

The last few statements you have a incorrect. You need to use with the appropriate indentation.
# Process: Append
      arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings)

except Exception as e:
      print e.message
      arcpy.AddError(e.message)
0 Kudos
meriyalootka
Occasional Contributor
The last few statements you have a incorrect. You need to use with the appropriate indentation.
# 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 (import_zip_91.py, line 40)

What is appropriate indentation?
Please help me more. Is it possible to send me your model or your code?
Regards
0 Kudos
BrianLeroux
Frequent Contributor
Hi
I tested your code, but it return this error:
IndentationError: unindent does not match any outer indentation level (import_zip_91.py, line 40)

What is appropriate indentation?
Please help me more. Is it possible to send me your model or your code?
Regards


It is pretty simple at this point. You just need to make sure your indentation is correct on those lines that I posted last. Delete the white space before each line so they are not indented and then tab each line over 1 time. The only exception is "except Exception as e:": should not be indented at all.
0 Kudos