I've been struggling with this problem for a few days. It seems to be more programmatic than geospatial in nature, so I'm open to suggestions to redirect this elsewhere.
I have a text file representing a project file for another program. I'm trying to search that text file based on a feature class (or shapefile) with an ID field (ATTR_1
) with elevation information (ElevMeters
) and replace default terrain heights with heights pulled from the feature. I can't really search for the ID info alone because it appears numerous times throughout the project file. From my perspective, I have to search through the project to find lines with $TERRAIN_HEIGHT :
, look three lines above to match $PHOTO_NUM
with the equivalent ATTR_1
, and then replace the $TERRAIN_HEIGHT
value with ElevMeters
.
I've gotten my script to property assign and print correct ElevMeters
values for each ATTR_1
found. I've been unable to get it to replace the $TERRAIN_HEIGHT :
line properly. It prints all new values at the end of the document rather than the line I thought I was directing it to.
import os, arcpy
pdir = r"input directory path"
infile = r"input project file path"
infc = r"input feature class"
fields = ['ATTR_1', 'ElevMeters']
fpath = os.path.join(pdir, infile)
f = open(fpath, 'r+')
searchlines = f.readlines()
for i, line in enumerate(searchlines):
if "$TERRAIN_HEIGHT : " in line:
print i #prints line number
for l in searchlines[i-3:i-2:2]: #go up three lines, skip every other character
print l #prints photo_num
print line #prints original terrain_height
with arcpy.da.SearchCursor(infc, fields) as cursor:
for row in cursor:
if str(row[0]) in l:
newline = str(line)[:20] + str(row[1])
f.write(line.replace(line, newline))
f.close()
To prove that I'm grabbing values correctly, when I run this code:
for i, line in enumerate(searchlines):
if "$TERRAIN_HEIGHT : " in line:
print i,
for l in searchlines[i-3:i-2:2]:
print l,
print line,
with arcpy.da.SearchCursor(infc, fields) as cursor:
for row in cursor:
if str(row[0]) in l:
newline = str(line)[:20] + str(row[1])
print newline
I get an output that looks something like this, with line number, photo number, a default $TERRAIN_HEIGHT :
of 650.000000
and correct new values underneath:
...
4153 $PHOTO_NUM : 60858
$TERRAIN_HEIGHT : 650.000000
$TERRAIN_HEIGHT : 446.301
4165 $PHOTO_NUM : 60859
$TERRAIN_HEIGHT : 650.000000
$TERRAIN_HEIGHT : 447.882
4181 $PHOTO_NUM : 60860
$TERRAIN_HEIGHT : 650.000000
$TERRAIN_HEIGHT : 448.934
4197 $PHOTO_NUM : 60861
$TERRAIN_HEIGHT : 650.000000
$TERRAIN_HEIGHT : 441.156
...