0
votes

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
@jonrsharpe - Thank you for the edits..Akhil
You may want to ask your question and post your code over at gis.stackexchange.com also.wwii
Yes.. Absolutely.. Thank you :)Akhil
Any assistance with the clipping segment is most welcome..Akhil

2 Answers

0
votes
  • Find the points/vertices that describe the line at the left edge - (x1, y1), (x2, y2)
  • Add a constant to the x values - (x1+k, y1), (x2+k, y2)
  • Find the y values on the polygon at the new x values - (x1+k, y3), (x2+k, y4)
  • Draw the line between those two points.
0
votes

Finally figured out the code to my question.. !! Thus answering it... 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")

This saves the result in a shapefile.

As a continuation to the above mentioned question, the solution of the problem is available at Clipping Line shapefiles within extent of Polygon shape. I think that this set of questions can now be considered answered and closed.

Thank you all for your assistance.