Posts Tagged ‘report’
Creating a list of keywords from the attribute table: New & Improved Version
Problem: You need a better way to summarize the contents of a dataset without wasting time typing a long list.
Back in March, I posted about creating a list of keywords from the attribute table. I knew that:
The solution I have come up with is not perfect. I keep thinking there must be a better way.
I had explained how to make a list of the administrative area types in the Natural Earth Admin 1 shapefile, using ArcMap’s “Summarize” tool. The end result was a text file that had extra quotation marks, commas, and other characters to remove.
There is a better way — with Python! I now have the power to structure the text file the way I want it from the start. Here’s the entire code:
import arcgisscripting
gp = arcgisscripting.create(9.3)
report = file(r"C:\gis\AdminTypes.txt", "w")
shape = r"C:\gis\10m-admin-1-states-provinces-shp.shp"
cur = gp.SearchCursor(shape)
row = cur.Next()
adminList = []
while row <> None:
adminList.append(row.ENGTYPE_1)
row = cur.Next()
adminList = list(set(adminList))
adminList.sort()
for admin in adminList:
report.write(admin + "\n")
report.close()
print "Finished"
Change only the portions marked in red to make this work with any data layer. They represent, in order from top to bottom:
- The output text file
- The input data layer
- The name of the attribute table field to summarize
The text file generated by this code is a list of each unique entry in the ENGTYPE_1 field (all duplicates removed) — one per line, in alphabetical order. It’s the exact same thing you would see in the table of contents if you symbolized by category on that field. Only now it’s in a form you can copy and paste into other places, like a metadata record.
Python makes everything better.
Tags: attribute table, keyword, list, python, report, text file
Creating a list of keywords from the attribute table
Problem: You need to summarize the contents of a dataset without wasting time typing a long list.
Natural Earth is a crisp basemap dataset available for the world at 1:10m, 1:50m, and 1:110m scales. I downloaded one of their most detailed layers, the 1:10m Admin 1 – States, Provinces shapefile for use as my example.
If you were asked to write a metadata record or some other type of report about this dataset, you would want your description to be as complete as possible. You might find it necessary to generate a long list of place or topic keywords. Often, the attribute table already includes this information. You just need a good way to extract it.
For instance, if I symbolize by category on the ENGTYPE_1 field of my natural earth layer, I get a long list of administrative area types. There’s way more than just states and provinces here! If only I could select this list and paste it somewhere else to have in a usable format. Unfortunately, it’s not that easy.
The solution I have come up with is not perfect. I keep thinking there must be a better way. If any of my readers know of one, I would love the feedback. For now, this method certainly beats having to type the entire list:
First, open attribute table and right click on the field you want to turn in to a keyword list. Then, select Summarize…
Chose one summary statistic field. It doesn’t matter which, but works a little better if you chose something that’s always going to be the same. I chose “CheckMe” because that’s basically a flag (yes or no) field. It doesn’t matter which summary statistic (minimum, maximum, first, last etc.) you pick either. You won’t be using any of this information–it’s just a way to get ArcMap to make a list for you that includes each category one time.
Hit the browse button in the Specify output table section. Then, change Save as type to Text File.
Open up your text file in Notepad and remove the first line (the field names). Then, do Edit — > Replace and replace all commas, quotation marks, numerals 1-9 and whatever text phrase was in your summary statistic field with nothing.
When you are done, you end up with this. If you want to add commas and move these all into one row, it’s pretty easy from here.
Tags: attribute table, keyword, list, report, summarize, text file