Exemplo n.º 1
0
        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!')
        eastWest = None
    if eastWest is not None:
        outputLatLon = crhMap.osgb2wgs(ngr)
        msg('Lat, Lon     >>>> {}'.format(outputLatLon))
else:   # input file argument provided
    processed, lineTtl, ignoreTtl = processInputFile(inputFile)
    if outputFile is None:
        msg('\n>>>>no output file specified...')
        for line in processed:
            if not ngr2LatLon:
                if extend:
                    if bsv:
                        msg(tuple2bsv(line))
                    else:
                        msg(tuple2csv(line))
                else:
                    msg(line[0])
            elif bsv:
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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)

print "\n5.0 osgb2wgs(({}, {}))     >> ".format(east, north) + str(cm.osgb2wgs(east, north))
print "5.1 osgb2wgs(('{}'))           >> ".format('SK1964') + str(cm.osgb2wgs('SK1964'))
print "    >> validNGR('SK1964'): {}".format(str(cm.validNGR('SK1926064482')))
print "5.2 osgb2wgs(('{}'))         >> ".format('SK192644') + str(cm.osgb2wgs('SK192644'))
print "5.3 osgb2wgs(('{}'))       >> ".format('SK19266448') + str(cm.osgb2wgs('SK19266448'))
print "5.4 osgb2wgs(('{}'))     >> ".format('SK1926064482') + str(cm.osgb2wgs('SK1926064482'))
print "    >> validNGR('SK1926064482'): {}".format(str(cm.validNGR('SK1926064482')))

print '\n6.0 deg2dms(1.33)    >> ' + str(cm.deg2dms(1.33))
print '6.0 deg2dms(1.3333)  >> ' + str(cm.deg2dms(1.3333))
print '6.2 deg2dms(1.99)    >> ' + str(cm.deg2dms(1.99))
print '6.3 deg2dms(1.9999)  >> ' + str(cm.deg2dms(1.9999))
print '6.4 deg2dms(-1.33)   >> ' + str(cm.deg2dms(-1.33))
print '6.5 deg2dms(-1.3333) >> ' + str(cm.deg2dms(-1.3333))
print '6.6 deg2dms(-1.99)   >> ' + str(cm.deg2dms(-1.99))
print '6.7 deg2dms(-1.9999) >> ' + str(cm.deg2dms(-1.9999))