Select to view content in your preferred language

Scripts to convert text file to polygon.

4488
6
10-22-2012 07:14 AM
DanielWebb1
Emerging Contributor
I am try to learn python and need to deal with the following:
At the end of each day the two foresters upload the gps data to you by ftp so you can process and re-format the data. The data comes to you as a text file (AS.txt and SB.txt). The text file field columns are: ID, Common Name, Diameter at Breast Height (DBH), Staff, Longitude, & Latitude. There are some errors within the data that need fixed:
â?¢ Spelling errors within Common Name field â?? here all names should be spelled correctly and capitalized.
â?¢ DBH 0 values â?? for collector AS a DBH of 0 should be changed to 14, AND for collector SB a DBH of 0 should be changed to 16.
Develop a script or set of scripts to automate the quality control and re-formatting steps. If you can help I can send the text files. I am trying to put together the complete script syntax.
I am having a tough time gathering the scripts I need.
0 Kudos
6 Replies
by Anonymous User
Not applicable
Original User: mzcoyle

You want to convert this data to a polygon? That doesn't make much sense to me since you would lose all this data you are trying to format.

Since your data doesn't have column headers it makes it a little trickier. You could add them to your csv file first and do the rest of your formatting there, and then load it directly, or you could create an empty feature class using your known columns and load the data then format it there.
0 Kudos
DanielWebb1
Emerging Contributor
From what I am told this is the steps of code I need to do this.
1. Create new shapefile that adds columns ID, DBH, Staff, Lattitude, Longitude. Name shapefile.
2. Prompting file to use.
3. Adding data from pulled file to new shapefile.
4. Correct spelling in common Fieldname, prompt for correction.
5. If staff = as, and = 0, change value to 14
6. If staff = SB, and DBH = 0, change value to 16
7. merge to master shapefile.
Does this make sense or am I not seeing something here? I am learning this so all help is appreciated.
0 Kudos
by Anonymous User
Not applicable
Original User: mzcoyle

This should get you started. You will want to create a script or function to create a template feature class if one does not exist, depending on if you want to make that a part of the code or as a separate process. The rest of the steps are pretty straight forward. You can create a list or dictionary of words and test against the list to see if they match.
import csv
import os
import arcpy

def main(input_csv, output_shapefile):

    workspace = r"C:\temp\py_temp"
    arcpy.env.workspace = workspace
    shapefile = r"C:\temp\py_temp\import_template.shp"

    out_path = workspace
    out_name = output_shapefile
    geometry = "POINT"
    template = shapefile
    arcpy.CreateFeatureclass_management(out_path, out_name, geometry, template)

    i_curs = arcpy.InsertCursor(out_name)
    with open(input_csv, "rb") as csvfile:
        open_reader = csv.reader(csvfile, delimiter=',')
        for blank, id, Name, DBH, Staff, Longitude, Latitude in open_reader:
            i_row = i_curs.newRow()
            i_row.ID = id
            Name = Name.capitalize()
            i_row.Name = Name
            i_row.DBH = DBH
            i_row.Staff = Staff
            i_row.Longitude = Longitude
            i_row.Latitude = Latitude
            i_curs.insertRow(i_row)

    del open_reader
    del i_curs
    del i_row

    print "done"

if __name__ == '__main__':
    csv_file = r"C:\temp\py_temp\SB.txt"
    shp_name = "shapefile2.shp"
    main(csv_file, shp_name)


Edit: Here is a very simple name check test. This simply assumes if the first letter matches the first letter in the list of tree types then that is the one to use. If you eventually have a list of tree types that have the same first letter you may want to make this more robust.

    name_list = ["Ash", "Dogwood", "Hickory", "Maple", "Oak"]
    ...
            Name = Name.capitalize()
            if Name not in name_list:
                for name_proper in name_list:
                    if Name.startswith(name_proper[0]):
                        Name = name_proper
            i_row.Name = Name
0 Kudos
RaphaelR
Deactivated User
For the name matching you could also try difflib.getclosematches()

import difflib
...
Name = difflib.get_close_matches(Name, ["Ash", "Hickory", "Oak", "Dogwood", "Maple"])[0]
0 Kudos
by Anonymous User
Not applicable
Original User: clthompson

I'm also confused, these look like individual points, not the verticies along the boundary of a traverse that might describe a stand or a plot.  The easiest thing you could do with those is to create an XY theme, and then use select by attribute combined with the field calculator to make some of the adjustments, and then copy the rows the of the event theme into a new shapefile.  This will create a point shapefile for you attributed with the column headers you specified.  Can you clarify the point versus polygon issue?
0 Kudos
DanielWebb1
Emerging Contributor
Ok I have clarity now. There is no polygon, just points. Also, this project is done in Modelbuilder, which creates the python script and usable text files. Thanks to all that helped.
0 Kudos