1

I am working with a GIS problem using a single input of a polygon shapefile.

Consider an irregular polygon. I want to draw vertical lines across the extent of the polygon at equal spacing.

How I intend to proceed is:

  1. Identify the bounding box (done using PyShp)

  2. Draw vertical Lines parallel to the left edge of the bounding box at equal spacing (How?)

  3. Clip the lines to the extent of the polygon (How, without using ArcPy?)

Note: They are required to be only vertical, and not a graticule. Also, I do not intend to use ArcPy, and intend to perform the coding completing in Python (2.7) as this segment of code needs to go into a tool generated from PyQt.

2

you could used shapely, here are some pieces of code that should help you build your script

from shapely.geometry import Point, LineString

for x in range(size):
    line = LineString([Point(leftX + x, 0), Point(leftX + x, 10000)])
    clippedline = line.intersection(polygon)
  • Could you please look into my code below, and suggest how to clip the resultant shapefile and the input polygon? – Akhil Sep 12 '14 at 9:21
2

Finally figured out the code to my question.. !! Thus answering it PARTIALLY... Thank you for your inputs..

Ipath = raw_input("Enter the input file :- ")
Opath = raw_input("Enter the output directory :- ")
Ipath = Ipath.replace("\\", "/") # Python requirement for paths
Opath = Opath.replace("\\", "/")
copyfile(str(Ipath) + ".prj", str(Opath) + "/" + "Out_Lines" + ".prj") # Copying projection file


sf = shapefile.Reader(str('Input Path'))
shapes = sf.shapes()
Box = shapes[0].bbox
Spc = input("Enter the grid spacing :- ") # Grid Spacing read

x_min = Box[0] # Save the coordinates of the right-bottom, left-top bounding box
y_min = Box[1]
x_max = Box[2]
y_max = Box[3]

A_bbox = [x_min, y_min] # First assignment of coordinates
B_bbox = [x_max, y_max]
C_bbox = [x_min, y_max]
D_bbox = [x_max, y_min]

w = shapefile.Writer(shapefile.POLYLINE) # Shapefile writer
w.line(parts = [[A_bbox, C_bbox]])
w.field('Path number', 'C', '50') 
w.record(str(1)) # Writes the first line, that is the left 'side' of the bounding box

# Increasing the X coordinate to generate a line at a specified spacing 
i = 2
while (A_bbox[0] <= x_max):
    A_bbox = [A_bbox[0] + Spc, A_bbox[1]]
    C_bbox = [C_bbox[0] + Spc, C_bbox[1]]
    w.line(parts = [[A_bbox, C_bbox]])
    w.record(str(i))
    i = i+1

w.save(str(Opath) + "/" + "Out_Lines")

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.