ใน ep นี้เราจะมาเรียนรู้วิธีการใช้ประโยชน์จากแผนที่มากขึ้น การจัดการข้อมูล Geospatial Data หรือข้อมูลภูมิศาสตร์ ที่เรามักพบบ่อย คือ Geocode แปลงข้อมูลในงานของเราให้เป็นข้อมูลภูมิศาสตร์ และการ Join ข้อมูลภูมิศาสตร์หลาย ๆ รูปแบบเข้าด้วยกัน ทั้ง Table Join และ Spatial Join

Geocode คืออะไร

Geocode คือกระบวนการแปลงข้อความ ไม่ว่าจะเป็น ชื่อ หรือที่อยู่ ให้เป็น พิกัดภูมิศาสตร์บนพื้นผิวโลก เช่น Latitude, Longitude ที่เราสามารถใช้ในการวางจุด Marker ลงบนแผนที่

ตัวอย่างเช่น ถ้าเราเคยใช้ Google Maps ในการค้นหาสถานที่สำคัญแล้ว จากชื่อ หรือ คำค้นเกี่ยวกับสถานที่นั้น ๆ แสดงว่าเราเคยใช้ Geocode โดยแล้ว โดยที่เราไม่รู้ตัว

Reverse Geocode คือ การแปลงพิกัดภูมิศาสตร์ กลับเป็นข้อความระบุที่อยู่ที่ให้มนุษย์สามารถอ่านได้เข้าใจ

Spatial Join คืออะไร

geopandas geodataframe spatial join european top universities with europe country boundaries
geopandas geodataframe spatial join european top universities with europe country boundaries

Spatial Join คือ การรวมหลาย ๆ GeoDataFrame เข้าด้วยกัน ด้วยความสัมพันธ์ทางภูมิศาสตร์ Spatial Relationship ระหว่าง Object ใน geometry Column

ตัวอย่างเช่น เรามี GeoDataFrame ของมหาวิทยาลัยชั้นนำในทวีปยุโรป ที่มีพิกัดภูมิศาสตร์ที่เรา Geocode มาจากชื่อที่อยู่ของมหาวิทยาลัยนั้น ๆ ถ้าจะ Join กันแบบปกติกับตารางประเทศ เราก็ต้องมี Column เช่น COUNTRY_ID เป็นต้น แต่ในเคสนี้เราสามารถใช้ Spatial Join พิกัดของมหาวิทยาลัย กับขอบเขตภูมิศาสตร์ของประเทศเหล่านั้นได้ ด้วย gpd.sjoin()

Spatial Join คือวิธีการนึงที่จะเพิ่มข้อมูลให้ GeoDataFrame หนึ่ง จากอีก GeoDataFrame หนึ่ง ในการใช้งานจริงเรามักจะต้องสร้าง Feature ใหม่ ๆ ที่มีความหมายที่เราต้องการขึ้นมาจากการรวมข้อมูล Feature ต้นฉบับที่เรามีใน Dataset

Spatial Join ต่างกับ Table Join อย่างไร

The different types of SQL join and their outputs as determined by sets. Credit https://commons.wikimedia.org/wiki/File:SQL_Joins.svg
The different types of SQL join and their outputs as determined by sets. Credit https://commons.wikimedia.org/wiki/File:SQL_Joins.svg

Table Join คือการรวมข้อมูลหลาย ๆ DataFrame เข้าด้วยกัน ด้วย Index ที่มีค่าตรงกัน ด้วย pd.DataFrame.join() คล้ายกับ SQL Join เรียกว่า Attribute join

เมื่อเราทำ Atrribute Join กับ GeoDataFrame เราควรใช้ gpd.GeoDataFrame.merge() ดังตัวอย่างด้านล่าง ที่จะรวม GeoDataFrame ชื่อ europe_boundaries ที่เก็บขอบเขตภูมิศาสตร์ของประเทศแถบยุโรป เข้ากับ europe_stats ที่เก็บข้อมูลสถิติ ด้วย Feature ชื่อ name (on="name")

เรามาเริ่มกันเลยดีกว่า

Open In Colab

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.