开发者

How to use python to turn a .dbf into a shapefile

开发者 https://www.devze.com 2023-02-12 02:23 出处:网络
I have been scouring the internet trying to find a pythonic (sp?!) way to process this data.. Everyday we will recieve a load of data in .dbf format (hopefully) - we then need to save this data as a

I have been scouring the internet trying to find a pythonic (sp?!) way to process this data..

Everyday we will recieve a load of data in .dbf format (hopefully) - we then need to save this data as a shapefile.

Does anyone have开发者_如何学运维 any links or any suggestions as to my process?


To append the file's creation_date to its name, you need to obtain the creation date with os.stat() and then rename the file with os.rename(). You can format the date string with date.strftime().

import datetime, os

filename = 'original.ext'

fileinfo = os.stat(filename)
creation_date = datetime.date.fromtimestamp(fileinfo.st_ctime)

os.rename(filename, filename + '-' + creation_date.strftime('%Y-%m-%d'))


Off the top of my head:

import os
import datetime
myfile = "test.txt"
creationdate = os.stat(myfile).st_ctime
timestamp = datetime.datetime.fromtimestamp(creationdate)
datestr = datetime.datetime.strftime(timestamp, "%Y%m%d")
os.rename(myfile, os.path.splitext(myfile)[0] + datestr + os.path.splitext(myfile)[1])

renames test.txt to test20110221.txt.


It was in model builder all along!

#   (generated by ArcGIS/ModelBuilder)
# Usage: DBF2SHAPEFILE <XY_Table> <Y_Field> <X_Field> <Output_Feature_Class>
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting, datetime

# Adds the creation date to all of the previous shapefiles in that folder
filename = 'D:/test.txt'
fileinfo = os.stat(filename)
creation_date = datetime.date.fromtimestamp(fileinfo.st_ctime)
os.rename(filename, filename + '-' + creation_date.strftime('%Y-%m-%d'))

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Script arguments...
XY_Table = sys.argv[1]

Y_Field = sys.argv[2]

X_Field = sys.argv[3]

Output_Feature_Class = sys.argv[4]

# Local variables...
Layer_Name_or_Table_View = ""

# Process: Make XY Event Layer...
gp.MakeXYEventLayer_management(XY_Table, X_Field, Y_Field, Layer_Name_or_Table_View, "")

# Process: Copy Features...
gp.CopyFeatures_management(Layer_Name_or_Table_View, Output_Feature_Class, "", "0", "0", "0")


If you wanted to do it without using ArcGIS, you could use OGR's python bindings or the ogr2ogr utility through a subprocess. You could use the utility through a windows batch file, which would be a lot faster than calling the arc process for every file if you have many to do...

As you know it's not a question of changing the extension, there is a specific format required.

0

精彩评论

暂无评论...
验证码 换一张
取 消