#!/usr/user/local/bin/python2.7 import os from scipy.interpolate import griddata from numpy import genfromtxt import matplotlib.pyplot as plt import numpy as np from shapely.geometry import mapping, Polygon, LineString, MultiLineString import fiona path = os.getcwd() my_data = genfromtxt( path + '/WaterTable.csv', delimiter=',') z = my_data[1:,2:3] x = my_data[1:,0] y = my_data[1:,1] xmax = max(x) xmin = min(x) ymax = max(y) ymin = min(y) xi = np.linspace(xmin, xmax, 70) yi = np.linspace(ymin, ymax, 70) # grid the data. zi = griddata((x, y), z.ravel(), (xi[None,:], yi[:,None]), method='cubic') print(zi) print(zi.shape) print(xi.shape) print(yi.shape) # contour the gridded data cs = plt.contour(xi,yi,zi,20) #,linewidths=2,colors='k') #cs = plt.contourf(xi,yi,zi,20) #,cmap=plt.cm.jet) plt.colorbar() # draw colorbar # plot data points. plt.scatter(x,y,marker='o',c='b',s=5) plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) #plt.title('griddata test (%d points)') # % npts) plt.show() #def get_contour_verts(cn): # contours = [] # # for each contour line # for cc in cs.collections: # paths = [] # # for each separate section of the contour line # for pp in cc.get_paths(): # xy = [] # # for each segment of that section # for vv in pp.iter_segments(): # xy.append(vv[0]) # paths.append(np.vstack(xy)) # contours.append(paths) # return contours lenvar = (len(cs.collections)) #number of contours print(lenvar) print(cs.collections) lines = [] # Empty list for contour sections for i in range(lenvar): n = i p = cs.collections[i].get_paths()[0] v = p.vertices # getting the individual verticies as a numpy.ndarray x = v[:,0] #takes first column y = v[:,1] #takes second column line = LineString([(i[0], i[1]) for i in zip(x,y)]) #Defines Linestring #varnam = ("contour_%d" %n) lines.append(line) #appends to list schema = {'geometry': 'LineString','properties': {'id': 'int'}} # sets up parameter for shapefile with fiona.open('ConShp.shp', 'w', 'ESRI Shapefile', schema) as c: # creates new file to be written to for j in range(len(lines)): l = (lines[j]) # creates variable print(l) print(type(l)) c.write({'geometry': mapping(l),'properties': {'id': j},})
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question