Created
June 24, 2017 13:07
-
-
Save is/7a71480d705d4993abcb9b7cf30c4d33 to your computer and use it in GitHub Desktop.
lanlon-processor
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
| import re | |
| import xlwings as xw | |
| def degree__convert_0(degree, minute, second): | |
| degree = float(degree) | |
| minute = float(minute) | |
| second = float(second) | |
| return "%.11f" % (degree + minute / 60.0 + second / 3600.0) | |
| LONLAT_PROCESSORS = [] | |
| def __LLP_ADD__(id, s, rp, op): | |
| LONLAT_PROCESSORS.append({ | |
| 'id':id, | |
| 'sample':s, | |
| 're':rp, | |
| 'op':op}) | |
| # ==== | |
| id = 'A0' | |
| s = '112.86277777777777' | |
| r = re.compile(r'^(\d{1,3})\.(\d+)$') | |
| def m(match): | |
| return [1, 'A0', match[0]] | |
| __LLP_ADD__(id, s, r, m) | |
| # ==== | |
| id = 'A1' | |
| s = '112.49.19' | |
| r = re.compile(r'(\d{1,3})\.(\d{2}).(\d{2})') | |
| def m(match): | |
| return [1, 'A1', match[1] + "." + match[2] + match[3]] | |
| __LLP_ADD__(id, s, r, m) | |
| # ==== | |
| id = 'B1' | |
| s = '''113°23′54″''' | |
| r = re.compile(r'''^(\d{1,3})°(\d+)[′'](\d+)[″"〃]$''') | |
| def m(match): | |
| return [1, 'B1', degree__convert_0(match[1], match[2], match[3])] | |
| __LLP_ADD__(id, s, r, m) | |
| # ==== | |
| id = 'B2' | |
| s = '''113°16'31''' | |
| r = re.compile(r'''^(\d{1,3})°(\d+)[′'](\d+)$''') | |
| def m(match): | |
| return [1, 'B2', degree__convert_0(match[1], match[2], match[3])] | |
| __LLP_ADD__(id, s, r, m) | |
| # ==== | |
| id = 'B3' | |
| s = '''113°23′54.33″''' | |
| r = re.compile(r'''^(\d{1,3})°(\d+)[′'](\d+\.\d+)″$''') | |
| def m(match): | |
| return [1, 'B3', degree__convert_0(match[1], match[2], match[3])] | |
| __LLP_ADD__(id, s, r, m) | |
| # ==== | |
| id = 'B4' | |
| s = '''113°62′''' | |
| r = re.compile(r'''^(\d{1,3})°(\d+)[′']$''') | |
| def m(match): | |
| return [1, 'B4', degree__convert_0(match[1], match[2],'0')] | |
| __LLP_ADD__(id, s, r, m) | |
| # ==== | |
| id = 'B5' | |
| s = '''113°3'7"''' | |
| r = re.compile(r'''^(\d{1,3})°(\d+)[′']$''') | |
| def m(match): | |
| return [1, 'B5', degree__convert_0(match[1], match[2],'0')] | |
| __LLP_ADD__(id, s, r, m) | |
| def lonlat_processor(s): | |
| s = s.strip() | |
| s = s.replace("'", "′").replace("'", "′") | |
| s = s.replace('"', '″').replace("〃", '″') | |
| for P in LONLAT_PROCESSORS: | |
| m = P['re'].match(s) | |
| if m != None: | |
| return P['op'](m) | |
| return [0, 'UNMATCH', '', '', '', ''] | |
| FN = 'shanxi.xlsx' | |
| wb = xw.Book(FN) | |
| sheet = wb.sheets[0] | |
| print(len(wb.sheets)) | |
| for sheet in wb.sheets: | |
| sheet.activate() | |
| row = 0 | |
| while True: | |
| row += 1 | |
| if sheet.range((row, 1)).value == None: | |
| break | |
| r = sheet.range((row, 7)) | |
| if r.value != None: | |
| res = lonlat_processor(str(r.value)) | |
| if res[0] > 0: | |
| sheet.range((row, 11)).value = res[1] | |
| sheet.range((row, 7)).color = (230,250, 230) | |
| sheet.range((row, 12)).value = res[2] | |
| else: | |
| sheet.range((row, 7)).color = (255, 255, 255) | |
| print ("%s %d: [%s]" % (sheet.name, row, r.value)) | |
| r = sheet.range((row, 8)) | |
| if r.value != None: | |
| res = lonlat_processor(str(r.value)) | |
| if res[0] > 0: | |
| sheet.range((row, 13)).value = res[1] | |
| sheet.range((row, 8)).color = (230,250,230) | |
| sheet.range((row, 14)).value = res[2] | |
| else: | |
| sheet.range((row, 8)).color = (255,255,255) | |
| print ("%s %d: [%s]" % (sheet.name, row, r.value)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment