일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 웹크롤링
- multinomiallogitregression
- 서울데이터
- platformurbanism
- 핫플레이스
- 네이버
- 파이썬
- postgres
- spacesyntax
- Python
- 베이지안
- QGIS
- 공간분석
- digitalgeography
- 도시설계
- graphtheory
- pandas
- 도시인공지능
- 그래프색상
- geodataframe
- 도시계획
- 그래프이론
- SQL
- 공간데이터
- 스마트시티
- 도시공간분석
- connectivity
- digital geography
- 서울
- naver
Archives
- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 웹크롤링
- multinomiallogitregression
- 서울데이터
- platformurbanism
- 핫플레이스
- 네이버
- 파이썬
- postgres
- spacesyntax
- Python
- 베이지안
- QGIS
- 공간분석
- digitalgeography
- 도시설계
- graphtheory
- pandas
- 도시인공지능
- 그래프색상
- geodataframe
- 도시계획
- 그래프이론
- SQL
- 공간데이터
- 스마트시티
- 도시공간분석
- connectivity
- digital geography
- 서울
- naver
Archives
- Today
- Total
이언배 연구노트
[Python + PostGIS] Dataframe 의 geometry 를 포함해서 PostGIS로 본문
Dataframe 은 어렵다
그래서 geodataframe 은 더 어렵다
그래서 geomdataframe 을 postgis로 보내는 건 더 어렵다.
일단, dataframe 가 geometry 를 가지고 있다고 가정하자.
csv, sql로 불러온 geometry column 은 'text' 형태로 불러진다.
우리는 text 를 geometry 로 바꿔줄 필요가 있다.
# text 형태의 geometry --> geometry object 형태의 geometry
# 내 데이터는 '01060000203B1400000100000001030000000' 이렇게 생겼었다. fromhex 를 쓸 때 주의할 것
df['geom'] = df['geom'].apply(lambda x: wkb.loads(bytes.fromhex(x)))
그리고 dataframe 은 멍청해서 저 geometry 도 이해 못하기 때문에, 'geodataframe' 으로 바꿔줘야
geometry column 을 품을 수 있다.
df['geom'] = df['geom'].apply(lambda x: wkb.loads(bytes.fromhex(x)))
gdf = gpd.GeoDataFrame(df, geometry = 'geom', crs = 'EPSG:5179') # 'geom' 칼럼을 인정받게 해주자
이제 우리는 postgres 로 보내줄 준비가 되었다.
지난번에 보였던 대로,
python --> PostGRES 는 sqlalchemy 를 쓰는 게 좋다
from sqlalchemy import create_engine
##################### Connection 을 세팅하자
account_identifier = '목표 컴퓨터' ## 나는 여기에 IP 주소를 입력했더니 통과되었다. localhost도 될듯
user = '아이디' ## Database 생성 시에 사용했던 ID 를 넣자
password = '패스워드' ##본인의 패스워드도 넣자
DB_name = '데이터베이스이름' ##접속하고자 하는 DB의 이름을 넣자
conn_string = f"postgresql://{user}:{password}@{account_identifier}/{DB_name}"
engine = create_engine(conn_string) ## 위 작성된 string 을 바탕으로 connection 을 위한 instance 를 만들었다.
##################### Connection 을 세팅하자
df['geom'] = df['geom'].apply(lambda x: wkb.loads(bytes.fromhex(x)))
gdf = gpd.GeoDataFrame(df, geometry = 'geom', crs = 'EPSG:5179')
# 여기서 흥미로운 건, 보통 sql로 보낼 때에는 'to_sql' 을 썼었는데, geometry 가 추가되고 'to_postgis' 함수를 쓴다는 거다
gdf.drop(columns = 'index').to_postgis('geo_data', con = engine, chunksize = 1000, if_exists = 'replace')
728x90
'Python' 카테고리의 다른 글
[QGIS + Python] 서울사람들은 어디로 놀러다닐까 (1) | 2025.01.24 |
---|---|
[Python] SHAP value 의 시각화 (1) | 2024.12.15 |
[Python] Classification 모델들 (0) | 2024.12.11 |
[Python] ANOVA F-Test, Multinomial Logit Regression (3) | 2024.12.10 |
[Python] NAVER API로 음식점 상세 정보 검색하기 (0) | 2024.10.15 |