Page 15 sur 25
Geopandas
Read a shape
[code]import geopandas as gpd
from tabulate import tabulate
myShape = 'C:\\Path\\Of\\My\\Shape.shp'
print('\n' + myShape)
df = gpd.read_file(myShape)
df['type'] = df['geometry'].astype(str).str.replace(r' .*|\(.*', '', regex=True)
df = df[['id', 'type', 'geometry']]
print(tabulate(df.head(10), headers='keys', tablefmt='psql', showindex=True))
print(df.shape[0])
MyFieldList = df['id'].drop_duplicates().dropna().sort_values(ascending=False).tolist()
print('\n' + str(MyFieldList))
MyGeomTypeList = df['type'].drop_duplicates().dropna().sort_values(ascending=False).tolist()
print('\n' + str(MyGeomTypeList))[/code]
Display a map
[code]from tabulate import tabulate
import matplotlib.pyplot as plt
MyLayer = 'E:\\Path\\to\\shape.shp'
df = gpd.read_file(MyLayer)
df.plot()
plt.title('My layer', pad=10, fontsize=10)
plt.show()[/code]
Display the Coordinate Reference Systems (CRS)
[code]print('roj : ' + str(df.crs))[/code]
Check geometry
[code]df['valid ?'] = df.is_valid
df = df[df['valid ?'] == False]
print(tabulate(df.head(5), headers='keys', tablefmt='psql', showindex=False))
print(df.shape[0])[/code]
Check projection
[code]if str(df.crs) == 'epsg:2154':
print(colored('OK: ' + str(df.crs), 'green'))
if str(df.crs) != 'epsg:2154':
print(colored('Warning: ' + str(df.crs), 'red'))[/code]
Create an empty shape
[code]import geopandas as gpd
myShape = 'C:\\Path\\beautiful shape.shp'
schema = {"geometry": "Polygon", "properties": {"myid": "int"}}
crs = "EPSG:2154"
dfEmpty = gpd.GeoDataFrame(geometry=[])
dfEmpty.to_file(myShape, driver='ESRI Shapefile', schema=schema, crs=crs)[/code]