def processInputFile(inputFile, lonField = 0): ''' process lat/lon input file assumed in csv or bsv format extracts fields lonField & lonfield +1 to calculate east, west & ngr values returns tuple of tuples, total line count and ignored line count actual elements in tuples depends on which of brief, standard (default) or extended is set ''' lineCount = ignoreCount = 0 lineLst = list() currentLineList = list() processedLineLst = list() latLonLst = list() inputLst = list() bsvInput = True for line in fileLineGen(inputFile): # crude bsv/csv check bsvInput = '|' in line if not bsvInput: if not ',' in line: statusErrMsg('fatal', 'processInputFile', 'unable to process input file: {}'.format(inputFile)) exit(1) if bsvInput: errMsg('bsv input file...', quiet) sepChar = '|' else: errMsg('csv input file...', quiet) sepChar = ',' break with open(inputFile, 'rb') as f: inputReader = csv.reader(f, delimiter = sepChar) for lineLst in inputReader: lineCount += 1 lineTpl = tuple(lineLst) if not brief: currentLineList = lineLst if len(lineTpl) > startField: latLon = [float(lineTpl[lonField - 1]), float(lineTpl[lonField])] eastWest = crhMap.wgs2osgb(latLon) try: ngr = crhMap.osgb2ngr(eastWest, precision) except RuntimeError as re: if verbose: errMsg('NGR >>>> Invalid input (line {})!'.format(lineCount), quiet) ngr = 'n/a' if extend: currentLineList.extend([str(eastWest[0]), str(eastWest[1]), ngr]) elif brief: currentLineList = [lineTpl[lonField - 1], lineTpl[lonField], ngr] else: currentLineList.append(ngr) processedLineLst.append(tuple(currentLineList)) else: ignoreCount += 1 continue return tuple(processedLineLst), lineCount, ignoreCount
def processInputFile(inputFile): ''' determine whether lat/lon or ngr input file & process it assumed in csv or bsv format for lat/lon values returns tuple of lat/long values tuples or tuple of ngr values, total line count and ignored line count actual elements in tuples depends on whether ngr or lat/long values given in input file ''' global ngr2LatLon lineCount = ignoreCount = 0 lineLst = list() currentLineLst = list() bsvInput = True for line in fileLineGen(inputFile): # crude ngr or lat/lon bsv/csv check if gridRef.match(line): ngr2LatLon = True errMsg('ngr input file...', quiet) else: ngr2LatLon = False bsvInput = '|' in line if not bsvInput: if not ',' in line: statusErrMsg('fatal', 'processInputFile', 'unable to process input file: {}'.format(inputFile)) exit(1) if bsvInput: errMsg('lat/lon input file (bsv)...', quiet) sepChar = '|' else: errMsg('lat/lon input file (csv)...', quiet) sepChar = ',' break if ngr2LatLon: # process the ngr input file for line in fileLineGen(inputFile): lineCount += 1 if gridRef.match(line): if crhMap.validNGR(line): latLonTpl = crhMap.osgb2wgs(line) if extend: latLonTpl = (line, str(latLonTpl[0]), str(latLonTpl[1])) else: latLonTpl = (str(latLonTpl[0]), str(latLonTpl[1])) else: if verbose: errMsg('East, West >>>> Invalid input ({})'.format(lineCount), quiet) if extend: latLonTpl = (line, 'n/a', 'n/a') else: latLonTpl = ('n/a', 'n/a') else: ignoreCount += 1 if extend: latLonTpl = (line, 'n/a', 'n/a') else: latLonTpl = ('n/a', 'n/a') currentLineLst.append(latLonTpl) else: # process the lan/lon input file with open(inputFile, 'rb') as f: inputReader = csv.reader(f, delimiter = sepChar) for lineLst in inputReader: lineCount += 1 if len(lineLst) == 2: latLonLst = [float(lineLst[0]), float(lineLst[1])] eastWest = crhMap.wgs2osgb(latLonLst) try: outputNgr = crhMap.osgb2ngr(eastWest, precision) except RuntimeError as re: if verbose: errMsg('invalid input (line {}): {}'.format(lineCount, str(lineLst), quiet)) outputNgr = 'n/a' if extend: currentLineLst.append((lineLst[0], lineLst[1], outputNgr)) else: currentLineLst.append((outputNgr,)) else: ignoreCount += 1 errMsg('invalid input (line {}): {}'.format(lineCount, str(lineLst), quiet)) if extend: lineLst.append('n/a') currentLineLst.append(tuple(lineLst)) else: currentLineLst.append(('n/a',)) return tuple(currentLineLst), lineCount, ignoreCount
errMsg('output sent to screen (stdout)', quiet) ## process data errMsg('') if args.latlon != '': # lat/lon (l) argument provided errMsg('single lat/lon reading provided...', quiet) latLon = args.latlon.split('|') if len(latLon) == 1: # not bsv record latLon = args.latlon.split(',') if len(latLon) == 1: # not csv record either statusErrMsg('fatal', 'args', 'lat/lon value must be either a csv or bsv pair: {}'.format(args.latlon)) exit(1) latLon = tuple([float(latLon[0]), float(latLon[1])]) msg('Lat, Lon >>>> {}'.format(latLon)) eastWest = crhMap.wgs2osgb(latLon) msg('East, West >>>> {}'.format(eastWest), quiet) try: outputNgr = crhMap.osgb2ngr(eastWest, precision) msg('NGR >>>> {}'.format(outputNgr)) except RuntimeError as re: msg('NGR >>>> Invalid input!') elif args.ngr != '': # ngr (n) argument provided errMsg('single ngr provided...', quiet) ngr = args.ngr msg('NGR >>>> {}'.format(ngr)) try: eastWest = crhMap.ngr2osgb(ngr) msg('East, West >>>> {}'.format(eastWest), quiet) except RuntimeError as re: msg('East, West >>>> Invalid input!')
# crhMap-Test.py -- mapping utilities tests # Copyright (c) 2016 CR Hailey # v1.00 crh 07-jan-16 -- initial release import crhMap as cm cm.fatalException = False # only sensible with bespoke exception handling code print 'crhMap.py -- mapping utilities (tier 3) tests\n' print '(53.17709 lat, -1.71328 lon) is a crossroads outside Youlgrave (SK19266448)...' (east, north) = cm.wgs2osgb(53.17709, -1.71329) print '\n1.0 east, north >> {}, {}'.format(east, north) print "\n2.0 wgs2osgb(53.17709, -1.71329) >> " + str(cm.wgs2osgb(53.17709, -1.71329)) print " >> validCoords(wgs2osgb(53.17709, -1.71329)): {}".format(str(cm.validCoords(cm.wgs2osgb(53.17709, -1.71329)))) print "2.1 wgs2osgb(100.12345, 100.12345) >> " + str(cm.wgs2osgb(100.12345, 100.12345)) print " >> validCoords(wgs2osgb(100.12345, 100.12345)): {}".format(str(cm.validCoords(cm.wgs2osgb(100.12345, 100.12345)))) print "\n3.0 ngr2osgb('SK19266448)') >> " + str(cm.ngr2osgb('SK19266448')) print "3.1 ngr2osgb('SK1926064482') >> " + str(cm.ngr2osgb('SK1926064482')) print "\n4.0 osgb2ngr(({}, {}), 4) >> ".format(east, north) + cm.osgb2ngr((east, north), 4) print "4.1 osgb2ngr(({}, {}), 6) >> ".format(east, north) + cm.osgb2ngr((east, north), 6) print "4.2 osgb2ngr(({}, {}), 8) >> ".format(east, north) + cm.osgb2ngr((east, north), 8) print "4.3 osgb2ngr((419260, 364482), 10) >> " + cm.osgb2ngr((419260, 364482), 10) print "4.4 osgb2ngr(({}, {})) >> ".format(east, north) + cm.osgb2ngr((east, north)) print "4.5 osgb2ngr((-704174, 4227357)) will generate an exception and continue..." try: cm.osgb2ngr((-704174, 4227357)) except RuntimeError as re: print "RuntimeError trapped: {}".format(re)