How to Create a new GeoDataFrame from scratch and save it into a shapefile
""" How to Create a new GeoDataFrame from scratch and save it into a shapefile
"""
import geopandas as gpd
from shapely.geometry import Point, Polygon
from pyproj import CRS
# Create an empty GeoDataFrame
newdata = gpd.GeoDataFrame()
""" We can see the output
Empty GeoDataFrame
Columns: []
Index: []
It's empty, but it must have at least one column which is the geometry column
"""
# Create an empty geometry column
newdata['geometry'] = None
print(newdata)
""" The output
Empty GeoDataFrame
Columns: [geometry]
Index: []
>>> newdata.columns.values
array(['geometry'], dtype=object)
"""
# Let's create a shapefile (shapely polygon) representing the Helsinki Senate Square, for that we need to create the coordinate of Helsinki
# Create coordinates
coordinates = [(24.950899, 60.169158), (24.953492, 60.169158), (24.953510, 60.170104), (24.950958, 60.169990)]
# Let's create a shapely polygon from the coordinate
poly = Polygon(coordinates)
print(poly)
# Let's insert this new data into our newdata geodataframe at first row which is row[0]
newdata.at[0, 'geometry'] = poly
# Let's plot our newdata
import matplotlib.pyplot as plt
newdata.plot()
plt.show()
""" The figure is showing us the visual interpretation of our polygon in a shape
"""
# Adding another column, location and name it 'Senaatintori'
newdata.at[0, 'Location'] = 'Senaatintori'
newdata.columns.values
# Determine the coordinate reference system (projection) for the newdata, because it's important since we need to have .prj file in out directory of shapefile files
newdata.crs
""" The result is empty, we need to add some crs information """
# Pass the coordinate: lon, lat in decimal degree, crs = WGS84, epsg: 4326
newdata.crs = CRS.from_epsg(4326).to_wkt()
"""
>>> newdata.crs
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
"""
# Now we have a shapefile from scratch with two columns, we can export it now and save it,
# Determine the location or output file
output_file = 'D:/Programm files/python/Scripts/Automatic Gis precess/lesson2/data_for_lesson_2/output_folder/newdata_newshapefile/finland_senate_location'
newdata.to_file(output_file)
Comments
Post a Comment