[TOC]
## 概況
### Background:把照片放在地圖上
我使用的Nokia Lumia 920沒有一個好的照片應用
### Showcase

Phodal’s Image
### 框架: EXIF & ExifRead & CartoDB
**EXIF**
> 可交換圖像文件常被簡稱為EXIF(Exchangeable image file format),是專門為數碼相機的照片設定的,可以記錄數碼照片的屬性信息和拍攝數據。
EXIF信息以0xFFE1作為開頭標記,后兩個字節表示EXIF信息的長度。所以EXIF信息最大為64 kB,而內部采用TIFF格式。
**ExifRead**
來自官方的簡述
> **Python library to extract EXIF data from tiff and jpeg files.**
**ExifRead安裝**
~~~
pip install exifread
~~~
**ExifRead Exif.py**
官方寫了一個exif.py的command可直接查看照片信息
~~~
EXIF.py images.jpg
~~~
**CartoDB**
> Create dynamic maps, analyze and build location aware and geospatial applications with your data using the power using the power of PostGIS in the cloud.
## 步驟
### Step 1: 解析讀取照片信息
簡單的來說,就是我們可以創建包含位置信息的內容到上面去。
主要步驟如下:
* 需要遍歷自己的全部圖片文件
* 解析照片信息
* 生成地理信息文件
* 上傳到cartodb
**python 遍歷文件**
代碼如下,來自于《python cookbook》
~~~
import os, fnmatch
def all_files(root, patterns='*', single_level=False, yield_folders=False):
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
if single_level:
break
~~~
**python 解析照片信息**
由于直接從照片中提取的信息是
~~~
[34, 12, 51513/1000]
~~~
也就是
~~~
N 34? 13' 12.718
~~~
幾度幾分幾秒的形式,我們需要轉換為
~~~
34.2143091667
~~~
具體的大致就是
~~~
def parse_gps(titude):
first_number = titude.split(',')[0]
second_number = titude.split(',')[1]
third_number = titude.split(',')[2]
third_number_parent = third_number.split('/')[0]
third_number_child = third_number.split('/')[1]
third_number_result = float(third_number_parent) / float(third_number_child)
return float(first_number) + float(second_number)/60 + third_number_result/3600
~~~
也就是我們需要將second/60,還有minutes/3600。
**python 提取照片信息生成文件**
~~~
import json
import exifread
import os, fnmatch
from exifread.tags import DEFAULT_STOP_TAG, FIELD_TYPES
from exifread import process_file, __version__
def all_files(root, patterns='*', single_level=False, yield_folders=False):
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
if single_level:
break
def parse_gps(titude):
first_number = titude.split(',')[0]
second_number = titude.split(',')[1]
third_number = titude.split(',')[2]
third_number_parent = third_number.split('/')[0]
third_number_child = third_number.split('/')[1]
third_number_result = float(third_number_parent) / float(third_number_child)
return float(first_number) + float(second_number)/60 + third_number_result/3600
jsonFile = open("gps.geojson", "w")
jsonFile.writelines('{\n"type": "FeatureCollection","features": [\n')
def write_data(paths):
index = 1
for path in all_files('./' + paths, '*.jpg'):
f = open(path[2:], 'rb')
tags = exifread.process_file(f)
# jsonFile.writelines('"type": "Feature","properties": {"cartodb_id":"'+str(index)+'"},"geometry": {"type": "Point","coordinates": [')
latitude = tags['GPS GPSLatitude'].printable[1:-1]
longitude = tags['GPS GPSLongitude'].printable[1:-1]
print latitude
print parse_gps(latitude)
# print tags['GPS GPSLongitudeRef']
# print tags['GPS GPSLatitudeRef']
jsonFile.writelines('{"type": "Feature","properties": {"cartodb_id":"' + str(index) + '"')
jsonFile.writelines(',"OS":"' + str(tags['Image Software']) + '","Model":"' + str(tags['Image Model']) + '","Picture":"'+str(path[7:])+'"')
jsonFile.writelines('},"geometry": {"type": "Point","coordinates": [' + str(parse_gps(longitude)) + ',' + str(
parse_gps(latitude)) + ']}},\n')
index += 1
write_data('imgs')
jsonFile.writelines(']}\n')
jsonFile.close()
~~~
### Step 2: 上傳數據
注冊CartoDB,然后上傳數據。
### 練習建議
無