TitlePages

Wednesday, 24 December 2014

Exporting the feature class to CSV Format


Exporting Geodatabase toComma Separated Value Format

Let start creating an python addin for converting the feature class in your geodatabase to CSV format.So that everyone can understand your feature class and its values.
  • If you are very new to python addin then go through- How to start with python addin 
  • The input feature class and output csv file path are hard coded in the sample program
  • For dynamic conversion get the path of the files through a variable and pass it as a parameter to the defined functions
Comma Seperated values
CSV Fromat



def ex_fc_to_csv(fc, out_csv):
    """
    Export all vertex in a line or poly feature class to a csv with OID
   
    example

        import geom_snippets
        geom_snippets.ex_fc_to_csv(r"c:\proj\fc1.shp", r"c:\proj\fc1.csv")

    output csv looks like this

        1,56019.99998067904,69118.00001450378
        1,56159.99998080942,69026.0000144181
        1,56359.999980995686,68913.00001431286
        2,34985.00002508866,68936.00001433428
        2,35178.000025268404,68805.00001421227

    """
    import csv
    import json

    with open(out_csv, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',', lineterminator='\n')

        with arcpy.da.SearchCursor(fc,
                                   field_names=("OID@","SHAPE@JSON")) as cursor:
            for row in cursor:
                geom = json.loads(row[1])
                for path in geom['paths']:
                    for pt in path:
                        csvwriter.writerow([row[0]] + pt)

No comments: