Skip to main content
1 of 7
Heinz
  • 1.6k
  • 5
  • 26
  • 42

Python: How to find out and edit specific rows of attribute table in a line shapefile?

I got a line shapefile tc_line with 166 rows (line segments), and the figure below was its atttribute table:

enter image description here

and with Fiona and networkx, I also got some edges in this shapefile (stored in numpy array) (How to get lines and nodes around the confluence point in a network system (line shapefile)?):

LINESTRING (260586.2596740723 2736302.815605165, 260586.2596740723 2736273.931404115)
LINESTRING (260586.2596740723 2736302.815605165, 260586.2596740723 2736331.699806215)
LINESTRING (260586.2596740723 2736302.815605165, 260615.1438751221 2736302.815605165)

I want to:

1. Add a column (type:float) containing 1 in the attribute table of tc_line

2. In the table, find out rows (line segments), which contain edges listed above

3. Update value in the new-added column of selected rows

But now I got stuck in stage 2 described above. I wanted to find out lines (rows in the attribute table) containing edges above by setting those edges as condition statement, but I don't know how to construct the condition statement:

import itertools
from shapely.geometry import Point, LineString
import numpy

G = nx.Graph()
for line in fiona.open('tc_line.shp'):
    ...

I am newbie to use Fiona and networkx, and how to solve this and achieve my objectives? I am glad to receive some tips.

I am working with python 2.7.12, Fiona 1.7.0 and networkx 1.11 under Win10 64bits.

Heinz
  • 1.6k
  • 5
  • 26
  • 42