Created
April 2, 2024 06:24
-
-
Save computational-sediment-hyd/9edf8ff48c8a64351994141b51d0bb9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "2c942b69", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from PIL import Image\n", | |
"import PIL.ExifTags as ExifTags" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5dfe6f45", | |
"metadata": {}, | |
"source": [ | |
"## 修正したプログラム" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "02320aef", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_gps(fname):\n", | |
" im = Image.open(fname)\n", | |
" # EXIF情報を辞書型で得る\n", | |
" exif = {\n", | |
" ExifTags.TAGS[k]: v\n", | |
" for k, v in im._getexif().items()\n", | |
" if k in ExifTags.TAGS\n", | |
" }\n", | |
" # GPS情報を得る --- (*2)\n", | |
" gps_tags = exif[\"GPSInfo\"]\n", | |
" gps = {\n", | |
" ExifTags.GPSTAGS.get(t, t): gps_tags[t]\n", | |
" for t in gps_tags\n", | |
" }\n", | |
" \n", | |
" # 変更箇所\n", | |
" conv_deg = lambda v : v[0] + (v[1] / 60.0) + (v[2] / 3600.0)\n", | |
" \n", | |
" lat = conv_deg(gps[\"GPSLatitude\"])\n", | |
" lat_ref = gps[\"GPSLatitudeRef\"]\n", | |
" if lat_ref != \"N\": lat = 0 - lat\n", | |
" lon = conv_deg(gps[\"GPSLongitude\"])\n", | |
" lon_ref = gps[\"GPSLongitudeRef\"]\n", | |
" if lon_ref != \"E\": lon = 0 - lon\n", | |
" \n", | |
" return lat, lon" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "7e427460", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(36.62291111111111, 138.24255277777777)" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"fname = 'XX.JPG'\n", | |
"get_gps(fname)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e26c3164", | |
"metadata": {}, | |
"source": [ | |
"## 応用編:フォルダ内の全ファイルのEXIF情報を取得し,geojson形式で出力" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "7e999049", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"import glob\n", | |
"import pandas as pd\n", | |
"import geopandas as gpd\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d13af66e", | |
"metadata": {}, | |
"source": [ | |
"### ファイルリストを取得" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "38c4bf23", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"path = r'C:\\\\Users'\n", | |
"fnames = glob.glob(path + '\\\\*.JPG')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "39239ce1", | |
"metadata": {}, | |
"source": [ | |
"### 位置情報を取得し,GeoDataFrameに格納" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "68127473", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"coords = [get_gps(f) for f in fnames]\n", | |
"c = np.array(coords)\n", | |
"\n", | |
"df = pd.DataFrame({\n", | |
"'lat':c[:,0] ,'lon':c[:,1]\n", | |
",'filename':[os.path.basename(f) for f in fnames]\n", | |
",'fullpath': fnames\n", | |
"})\n", | |
"\n", | |
"geometry = gpd.points_from_xy(df.lon, df.lat, crs=\"EPSG:4326\")\n", | |
"gdf = gpd.GeoDataFrame(df, geometry=geometry)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3556888b", | |
"metadata": {}, | |
"source": [ | |
"### geojson形式で出力" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "5a15dc59", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"out = gdf.to_file('photo.geojson', driver='GeoJSON')\n", | |
"del out" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.12" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment