2020년 4월 27일 월요일

[Python] GeoPandas


import geopandas as gpd
(conda install -c conda-forge geopandas -n envname)


< read, dataframe >

1. gdf = gpd.read_file(filesite)
2. df = pd.read_csv(filesite)
   gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.long, df.lat), crs={'init':'epsg:4326'})

* the equal-area projections : Lambert Cylindrical Eaul Area, Africa Albers Equal Are conic
* the equidistant projections : Azimuthal Equidstant projection


< draw >

ax = gdf.plot(figsize=(10, 10), color='whitesmoke', linestyle=':', edgecolor='black', zorder=1)
a.plot(color='maroon', markersize=10, zorder=2, ax=ax)


< re-projecting >

gdf.to_crs(epsg=32630)
regions.to_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")


< attribute >

* POINT
gdf.geometry.x
gdf.geometry.y 
* POLYGON
gdf.geometry.area
* Calculate the total area (in square kilometers)
totalArea = south_america.geometry.to_crs(epsg=3035).area.sum() / 10**6


< Geo Coding >

from geopandas.tools import geocode

result = geocode("The Great Pyramid of Giza", provider="nominatim")

def my_geocoder(row):
    try:
        point = geocode(row, provider='nominatim').geometry.iloc[0]
        return pd.Series({'Latitude': point.y, 'Longitude': point.x, 'geometry': point})
    except:
        return None

universities[['Latitude', 'Longitude', 'geometry']] = universities.apply(lambda x: my_geocoder(x['Name']), axis=1)

print("{}% of addresses were geocoded!".format(
    (1 - sum(np.isnan(universities["Latitude"])) / len(universities)) * 100))

# Drop universities that were not successfully geocoded
universities = universities.loc[~np.isnan(universities["Latitude"])]
universities = gpd.GeoDataFrame(universities, geometry=universities.geometry)
universities.crs = {'init': 'epsg:4326'}
universities.head()

< Spartial Join >

# Use spatial join to match universities to countries in Europe
european_universities = gpd.sjoin(universities, europe)

# Investigate the result
print("We located {} universities.".format(len(universities)))
print("Only {} of the universities were located in Europe (in {} different countries).".format(
    len(european_universities), len(european_universities.name.unique())))
< Porximaty Analysis >
# Measure distance from release to each station
distances = stations.geometry.distance(recent_release.geometry)
distances

two_mile_buffer = stations.geometry.buffer(2*5280)
 # Plot each polygon on the map
folium.GeoJson(two_mile_buffer.to_crs(epsg=4326)).add_to(m)

# Turn group of polygons into single multipolygon
my_union = two_mile_buffer.geometry.unary_union
print('Type:', type(my_union))

# Show the MultiPolygon object
my_union

# The closest station is less than two miles away
my_union.contains(releases.iloc[360].geometry)

# The closest station is more than two miles away
my_union.contains(releases.iloc[358].geometry

댓글 없음:

댓글 쓰기

200926.가오리코스 라이딩

9/26 골절인지 아닌지 확인 안됨. 이후 미세골절여부 확인 핸드폰을 드는 것도 어려움 9/29 x ray 다시 찍지 않고 이후 재 방문 요청 ...