Here are thousands of pictures in JPG or TIF format with world file such as jgw and tfw. Inside each file, projection information for each pictures were contained. One example of the content for a typical TFW file is showed in below:
0.17605
0.00000
0.00000
-0.17605
-57782.10446
-18285.74039
So, how can I transfer it into DBF through PYTHON? I want make a catalog file for display on ArcGIS.
Answer
Try script below. It is very old (2008) - one of my very 1st Python scripts, not elegant at all and can be rewritten using a fraction of code. It works, so never bothered to renew it.
Script takes 4 parameters as inputs. I hope you know how to define script parameters in parameters dialog.
- outputFolder - folder where table based image catalog will be saved. Type - folder, output,
- outputTable - name of output dBase table, parameter type string, e.g. imcat.dbf. Direction - input. It will be saved in outputFolder. It is your catalog to be displayed in ArcGIS.
- layoutTable - layout dBase table with 5 colimns: IMAGE, XMIN, YMIN, XMAX, YMAX. First field is string (250 long), the rest are doubles. Yes, user have to create this table first. I did it once, and use this tool countless number of times, pointing to the same layout table.
- inRasters - input raster, type raster dataset, multiple input. Use ArcCatalog to drag and drop multiple images onto it.
Some rasters don't use world files, tool handles them nicely/
import arcgisscripting, sys, os
import traceback
try:
gp = arcgisscripting.create()
# output folder to store the table
outputFolder = gp.GetParameterAsText(0)
# table name
outputTable = gp.GetParameterAsText(1)
# template table name
layoutTable = gp.GetParameterAsText(2)
inRasters = gp.GetParameterAsText(3)
# The list is split by semicolons ";"
inLayers = inRasters.split(";")
gp.CreateTable_management(outputFolder, outputTable, layoutTable, "")
repl=os.sep+os.sep
inRows = gp.insertcursor(outputFolder+os.sep+outputTable)
for inRaster in inLayers:
theR=inRaster.strip("'")
theR=theR.replace(os.sep,repl)
desc = gp.Describe(theR)
theCoords=desc.extent
theCoords=theCoords.split(" ")
xMin = float(theCoords[0])
xMax = float(theCoords[2])
yMin = float(theCoords[1])
yMax = float(theCoords[3])
inRow = inRows.newrow()
inRow.IMAGE = inRaster.strip("'")
inRow.XMIN = xMin
inRow.YMIN = yMin
inRow.XMAX = xMax
inRow.YMAX = yMax
inRows.insertrow(inRow)
gp.ADDMESSAGE (inRaster + "...processed")
del inRows
del inRow
gp.delete
print "Done"
except:
# get the traceback object
tb = sys.exc_info()[2]
# tbinfo contains the line number that the code failed on and the
# code from that line
tbinfo = traceback.format_tb(tb)[0]
# concatenate information together concerning the error into a
# message string
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
# Generate a message string for any geoprocessing tool errors
msgs = "GEOPROCESSOR ERRORS:\n" + gp.GetMessages(2) + "\n"
# Return gp messages for use with a script tool
gp.AddError(msgs)
print msgs
gp.AddError(pymsg)
print pymsg
No comments:
Post a Comment