2

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?

Alice Duff
  • 667
  • 6
  • 11
  • 18
  • No idea if this is accurate, but: "Published maps do not contain actual geographical data. Instead, they link to data sets at a central remote location, which may be hosted by an ArcMap Server or available over the Internet." (http://www.fileinfo.com/extension/pmf) So it might not be the pmf file you actually need to update. – Thomas K Feb 18 '11 at 12:46
  • too true thomas! thank you for pointing this out! – Alice Duff Feb 21 '11 at 07:31
  • i've edited my post with what is now my definate problem! – Alice Duff Feb 21 '11 at 09:03

4 Answers4

2

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'))
miles82
  • 6,584
  • 38
  • 28
1

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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • I got this error message:Message File Name Line Position Traceback C:\Documents and Settings\DuffA\Bureaublad\Magneto\Rename+Date.py 18 NameError: name '_' is not defined – Alice Duff Feb 21 '11 at 10:22
  • `_` only works this way in the interactive shell (it contains the result of the previous expression). In a script, you need to assign variables yourself. – Tim Pietzcker Feb 21 '11 at 10:26
  • im really sorry but i don't understand what you mean! my programming knowledge is limited! – Alice Duff Feb 21 '11 at 10:32
  • OK; I've changed the example to use variables. You'll still need to provide the names of the actual files to be renamed, of course. – Tim Pietzcker Feb 21 '11 at 10:47
0

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")
John Machin
  • 81,303
  • 11
  • 141
  • 189
Alice Duff
  • 667
  • 6
  • 11
  • 18
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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Benjamin
  • 11,560
  • 13
  • 70
  • 119