I have a script for creating points shapefiles, but I can not fix it for make a new polyline shp file.
I have obtained the data from a excel file and extract data in three differents lists
Lon,lat,label
import shapefile as shp
from openpyxl import load_workbook
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
r = 'myfile.xlsm'
wb = load_workbook(filename= r, data_only=True, use_iterators = True)
ws = wb['Export_Area']
cell_range = ws ['B5:D9']
tuple(ws.iter_rows('B5:D9'))
lista = []
for row in ws.iter_rows('B5:D9'):
for cell in row:
lista.append(cell.value)
Longitud_area = (lista[1:len(lista):3])
latitud_area = (lista[0:len(lista):3])
puntos_area = (lista[2:len(lista):3])
creating the shp file
out_file = 'ExportArea.shp'
x,y,id_no=Longitud_area,latitud_area,puntos_area
logic = [True,False,True]
w = shp.Writer(shp.POINT)
w.autoBalance = 1 #ensures gemoetry and attributes match
w.field('X','F',10,5)
w.field('Y','F',10,5) #float - needed for coordinates
w.field('etiqueta','D') #date
for j,k in enumerate(x):
w.point(k,y[j]) #write the geometry
w.record(k,y[j],puntos_area[j]) #write the attributes
creating the prj file.
prj = open(out_file +'.prj', 'w')
proyeccion ="GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
prj.write(proyeccion)
It works fine for a point shp file but I can not see how to do the loop for making a poly shp file
Best Answer
If you want the coordinates (longitud, latitud) in a single list, use the function
zip()
For reading an Excel file it is easiest now to use the Pandas module.
Point shapefile
Now, a polyline
Result
If you wish to go further, use GeoPandas (geospatial Pandas, the geometric operations are performed by shapely, file access by Fiona, not PyShp, and plotting by descartes and matplotlib)