Pregunta

I've been making little maps to orient myself to using shapely.

for example:

from shapely.geometry import MultiLineString 
from pprint import pprint
import pylab

coords = [((1,1),(1,13)),((3,1),(3,13)),((5,1),(5,13)),((7,1),(7,13)),((9,1),(9,13)), ((11,1),(11,13)),((13,1),(13,13)),((1,1),(13,1)),((1,3),(13,3)), ((1,5),(13,5)),((1,7),(13,7)),((1,9),(13,9)),((1,11),(13,11)),((1,13),(13,13)),((1,1),(13,13)),((3.5,1),(13,10.5)),((6.5,1),(13,7.5)),((9.5,1),(13,4.5)),((1,3.5),(10.5,13)),((1,6.5),(7.5,13)),((1,9.5),(4.5,13))]
lines_1 = MultiLineString(coords)
mow = lines_1.buffer(0.25)}

I discover I am a bad typist and it is difficult to write long lists of coordinates.

I noticed numpy mgrid and wondered if there is a way to use it to create arbitrary rectangular xy grids and then convert the mgrid to a list for shapely LineString.

The grid is generally designed to be something like a checker board. Some horizontal lines in the grid are then buffered to become polygons, then vertical lines buffered, then the two cascade unioned and I have my checker board. I then take different diagonals, union them with the checker board and extract the resultant polygons from the linear rings.

As the map dimensions get larger, and being not the best typist, I was hoping that there might be a way to use mgrid to do some of my typing much better.

ie. x, y = np.mgrid[:55, :35] # rectangular

This gives the expected x and y values. I an confused as to how I might take these results back to an Nx2 array for Linestring(s) as list for shapely.

Thank you in advance for any guidance. Chris

¿Fue útil?

Solución

You can you vstack combined with transpose:

>>> x, y = np.mgrid[:2, :3]
>>> np.vstack((x.ravel(),y.ravel())).T
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])

If you need a list of tuples:

>>> [(x,y) for x,y in zip(x.ravel(),y.ravel())]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top