Last active
April 14, 2017 08:30
-
-
Save jkatagi/8810803f27aea48a1c1273ce512a3d71 to your computer and use it in GitHub Desktop.
convert lat lon (latlon coordinate) to UTM using UTM zone info and cs2cs command.
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
# coding: utf-8 | |
# In[40]: | |
import pandas as pd | |
import csv | |
import subprocess | |
import re | |
# In[36]: | |
src_list = "test.csv" | |
#src_list = "./AV2PAN_UTM_zone.csv" | |
def run_echo(command): | |
proc = subprocess.Popen( | |
command, | |
stdout=subprocess.PIPE) | |
return proc | |
def run_cs2cs(command, input_stdin): | |
proc = subprocess.Popen( | |
command, | |
stdin=input_stdin, | |
stdout=subprocess.PIPE) | |
return proc | |
def run_grep(command, input_stdin): | |
proc = subprocess.Popen( | |
command, | |
stdin=input_stdin, | |
stdout=subprocess.PIPE) | |
return proc | |
def replace_tab_and_newline(string): | |
""" replace character tab and newline, and retrun lon lat.""" | |
pattern = r'(\S*)\t(\S*) 0.00\n' | |
matchOB = re.match(pattern, string) | |
if matchOB: | |
lon = matchOB.group(1) | |
lat = matchOB.group(2) | |
return lon, lat | |
# In[68]: | |
def convert_to_UTM(lon_latlon, lat_latlon, UTM_zone): | |
""" convert latlon to UTM latlon. | |
input: | |
lon_lalton : longitude of latlon coordinate. | |
lat_lalton : latitude of latlon coordinate. | |
UTM_zone : number of UTM. | |
return: | |
lon_UTM : longitude of UTM cooridnate. | |
lat_UTM : latitude of latlon coordinate. | |
""" | |
# echo lon lat | cs2cs -v +init=epsg:4326 + to +init=epsg:???? | grep -v # | |
echo_command = ["echo",lon_latlon,lat_latlon] | |
cs2cs_command = ["cs2cs", "-v", "+init=epsg:4326", "+to", "+init=epsg:" + str(UTM_EPSG_dict[UTM_zone])] | |
grep_command = ["grep", "-v", "#"] | |
# run above commands. | |
echo_proc = run_echo(echo_command) | |
cs2cs_proc = run_cs2cs(cs2cs_command, echo_proc.stdout) | |
grep_proc = run_grep(grep_command, cs2cs_proc.stdout) | |
# decode UTf-8 | |
out, err = grep_proc.communicate() | |
string = out.decode('utf-8') | |
# get lon_UTM, lat_UTM | |
lon_UTM, lat_UTM = replace_tab_and_newline(string) | |
return lon_UTM, lat_UTM | |
# In[78]: | |
UTM_EPSG_dict = { | |
"52":32652, | |
"53":32653, | |
"54":32654, | |
"55":32655} | |
def main(): | |
output_list = [] | |
with open(src_list, newline='') as csvfile: | |
reader = csv.reader(csvfile, delimiter=',') | |
header = next(reader) # skip header | |
for row in reader: | |
id = row[0] | |
category = row[1] | |
lon_latlon = row[2] | |
lat_latlon = row[3] | |
scene = row[4] | |
UTM_zone = row[5] | |
# convert UTM coordinate. | |
lon_UTM, lat_UTM = convert_to_UTM(lon_latlon, lat_latlon, UTM_zone) | |
# return list. | |
output_list.append([id, category, lon_UTM, lat_UTM, scene]) | |
# save as csv. | |
with open('50points_UTM.csv', 'w') as f: | |
writer = csv.writer(f, lineterminator='\n') | |
writer.writerows(output_list) | |
# In[79]: | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment