""" Térinformatikai algoritmusok Shapefile-ok kezelése Pythonban Máté Cserép """ import shapefile # pip3 install pyshp import matplotlib.pyplot as plt # pip3 install matplotlib # Open a shapefile (.shp) # dBase file of attributes (.dbf) is automatically detected by name convention # Source: http://gis.inf.elte.hu/files/public/elte-megye sf = shapefile.Reader('megye_region.shp') # Check whether files contains polygons if sf.shapeType == shapefile.POLYGON: print("This file contains polygons") # Print the number of shapes (geometries) in the file print("Number of counties: {0}".format(len(sf.shapes()))) print("------------------") # Start new plot figure plt.figure() # Iterate through all the shapes for shape in sf.shapes(): # Only consider the first polygon if multiple parts are defined end = len(shape.points) if len(shape.parts) == 1 else shape.parts[1] - 1 # Get the X an Y positions into separate lists xs = [coord[0] for coord in shape.points[:end]] ys = [coord[1] for coord in shape.points[:end]] # Add polygon to plot plt.plot(xs, ys) # Display plot ... plt.show() # ... or save plot #plt.savefig('map.png')