3

I am working on water network in ArcGIS 10.2. I am trying to insert data from a data.csv file to an existing feature class House Meter in personal GDB.

One method I know to use is the join data.csv file on the basis of common field with House meters.

After that use field calculator for data insertion. This method takes too much time because I have to insert data in almost 15 fields from CSV file.

I want to insert data without join data.csv file and quickly.

Does anyone know of a script or expression for this query?

2

So, you have a feature class, say, it has a number of point features. The feature class has 15 fields. Now you have a .csv file which contains the ID field (which can identify each point in your feature class HouseMeters in the unique way). Your goal is to calculate the field values in this feature class based on the values provided in the .csv files.

I would suggest using Python for this. There is a way to read the .csv file into a list of dictionaries, very handy one.

import unicodecsv
import os
import arcpy

file_name = r"C:\GIS\housemeters_import.csv"

with open(file_name,"rb") as f:
    reader = unicodecsv.DictReader(f)
    csv_rows = list(reader)

Now let's see what's inside the csv_rows.

print csv_rows[:2]
[{u'CID': u'11',
  u'FID': u'0',
  u'OBJECTID': u'1',
  u'POINT_X': u'-122.396707',
  u'POINT_Y': u'37.7855429999',
  u'ValueMetersPerHouse': u'5'},
 {u'CID': u'21',
  u'FID': u'1',
  u'OBJECTID': u'2',
  u'POINT_X': u'-122.411825',
  u'POINT_Y': u'37.7739790002',
  u'ValueMetersPerHouse': u'6'}]

What is left is to update fields in the feature class with the values from this list of dictionaries. This code should be adjusted to match the names of the fields you have in your .csv and feature class.

in_fc = r"C:\GIS\Temp\ArcGISHomeFolder\PersonalGDB.mdb\HouseMeters"

with arcpy.da.UpdateCursor(in_fc,["HouseID","MeasuredValue","MeasuredBy"]) as upd_cur:
    for feat in upd_cur:
        feat[1] = [i['ValueMetersPerHouse'] for i in csv_rows
                   if i['FID'] == str(feat[0])][0]
        feat[2] = [i['MeasuredByTechnicianName'] for i in csv_rows
                           if i['FID'] == str(feat[0])][0]

        upd_cur.updateRow(feat)

Since you have ca 15 fields, you would need to copy the

feat[2] = [i['MeasuredByTechnicianName'] for i in csv_rows
                               if i['FID'] == str(feat[0])][0] 

multiple times increasing the index in feat[n] by one for every new field and update the field names supplied inside the ' ' symbols.

Learn more about arcpy.da.UpdateCursor and arcpy in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.