I'm converting csv data to shapefiles using the pyshp python library. What I want to do is very similar to Using the python shape library: pyshp - how to convert .csv file to .shp, except that I don't know how to handle polygons.
My data is in a csv file. Each row corresponds to a named rectangle, i.e. a string and the latitude or longitude coordinates of each side of the rectangle: left side (xl), right side (xr), top (yt) and bottom (yb).
So my data looks like this:
name,xl,xr,yt,ybsome Name,-25.3125,22.5,47.517193,31.353634another Name,-103.359375,-0.703125,80.87282,74.40216...And my python code is pretty simple. It is only slightly modified from the points example. But when I try to import this data into google maps, it has some errors parsing it. So I think I'm doing something wrong, but I'm not sure what?
#Set up blank lists for dataname, polyPart = [],[]#read data from csv file and store in listswith open(in_file, 'rb') as csvfile: r = csv.reader(csvfile, delimiter=',') for i,row in enumerate(r): if i > 0: #skip header # parse data into a point array representing the bounding box xl = float(row[1]) xr = float(row[2]) yt = float(row[3]) yb = float(row[4]) tl = [xl, yt] tr = [xr, yt] br = [xr, yb] bl = [xl, yb] parr = [tl, tr, br, bl, tl] # array of one "part", the part is an array of points polyPart.append([parr]) name.append(row[0])#Set up shapefile writer and create empty fieldsmaxStringLength = 50w = shp.Writer(shp.POLYGON)w.field('name','C',maxStringLength)#loop through the data and write the shapefilefor j, name in enumerate(name): w.poly(parts=polyPart[j]) w.record(name)#Save shapefilew.save(out_file)
أكثر...
My data is in a csv file. Each row corresponds to a named rectangle, i.e. a string and the latitude or longitude coordinates of each side of the rectangle: left side (xl), right side (xr), top (yt) and bottom (yb).
So my data looks like this:
name,xl,xr,yt,ybsome Name,-25.3125,22.5,47.517193,31.353634another Name,-103.359375,-0.703125,80.87282,74.40216...And my python code is pretty simple. It is only slightly modified from the points example. But when I try to import this data into google maps, it has some errors parsing it. So I think I'm doing something wrong, but I'm not sure what?
#Set up blank lists for dataname, polyPart = [],[]#read data from csv file and store in listswith open(in_file, 'rb') as csvfile: r = csv.reader(csvfile, delimiter=',') for i,row in enumerate(r): if i > 0: #skip header # parse data into a point array representing the bounding box xl = float(row[1]) xr = float(row[2]) yt = float(row[3]) yb = float(row[4]) tl = [xl, yt] tr = [xr, yt] br = [xr, yb] bl = [xl, yb] parr = [tl, tr, br, bl, tl] # array of one "part", the part is an array of points polyPart.append([parr]) name.append(row[0])#Set up shapefile writer and create empty fieldsmaxStringLength = 50w = shp.Writer(shp.POLYGON)w.field('name','C',maxStringLength)#loop through the data and write the shapefilefor j, name in enumerate(name): w.poly(parts=polyPart[j]) w.record(name)#Save shapefilew.save(out_file)
أكثر...