示例#1
0
func osgbHandler(req *GEOConvertRequest, osgb36strval, oformat string) (interface{}, error) {
	var osgb36val *osgb36.OSGB36Coord
	var err error
	if osgb36val, err = osgb36.AOSGB36ToStruct(osgb36strval, osgb36.OSGB36Leave); err != nil {
		return nil, err
	}
	return serialize(osgb36.OSGB36ToWGS84LatLong(osgb36val), oformat)
}
示例#2
0
func main() {

	var ofcmdlinespec, ifcmdlinespec string
	var of displayformat
	var ifm inputformat
	var lines uint
	var instring, outstring, ofparamvalues, ifparamvalues string
	var pc *cartconvert.PolarCoord

	for key, _ := range ofOptions {
		ofparamvalues += fmt.Sprintf(" %s ", key)
	}

	for key, _ := range ifOptions {
		ifparamvalues += fmt.Sprintf(" %s ", key)
	}

	flag.StringVar(&ofcmdlinespec, "of", "deg", "specify output format. Possible values are: "+ofparamvalues)
	flag.StringVar(&ifcmdlinespec, "if", "osgb36", "specify input format. Possible values are: "+ifparamvalues)
	flag.Parse()

	of = ofOptions[strings.ToLower(ofcmdlinespec)]
	ifm = ifOptions[strings.ToLower(ifcmdlinespec)]

	reader := bufio.NewReaderSize(os.Stdin, 100)
	longline := false

	for data, prefix, err := reader.ReadLine(); err != io.EOF; data, prefix, err = reader.ReadLine() {
		if err != nil {
			fmt.Fprintf(os.Stderr, "conv %d: %s\n", lines, err)
			continue
		}

		if prefix {
			longline = true
			continue
		}

		if longline {
			longline = false
			continue
		}

		lines++

		instring = strings.TrimSpace(string(data))

		if len(instring) == 0 {
			continue
		}

		switch ifm {
		case ifbmn:

			bmncoord, err := bmn.ABMNToStruct(instring)

			if err != nil {
				fmt.Fprintf(os.Stderr, "BMN: error on line %d: %s\n", lines, err)
				continue
			}
			pc, err = bmn.BMNToWGS84LatLong(bmncoord)

			if err != nil {
				fmt.Fprintf(os.Stderr, "BMN: error on line %d: %s (BMN does not return a lat/long bearing)\n", lines, err)
				continue
			}
		case ifosgb36:
			osgb36coord, err := osgb36.AOSGB36ToStruct(instring, osgb36.OSGB36Auto)

			if err != nil {
				fmt.Fprintf(os.Stderr, "OSGB36: error on line %d: %s\n", lines, err)
				continue
			}
			pc = osgb36.OSGB36ToWGS84LatLong(osgb36coord)
		}

		switch of {
		case ofdeg:
			lat, long := cartconvert.LatLongToString(pc, cartconvert.LLFdeg)
			outstring = lat + ", " + long
		case ofdms:
			outstring = pc.String()
		case ofutm:
			outstring = cartconvert.LatLongToUTM(pc).String()
		case ofgeohash:
			outstring = cartconvert.LatLongToGeoHash(pc)
		default:
			fmt.Fprintln(os.Stderr, "Unrecognized output specifier")
			flag.Usage()
			fmt.Fprintf(os.Stderr, "possible values are: [%s]\n", ofparamvalues)
			fmt.Fprintln(os.Stderr, "]")
			os.Exit(2)
		}
		fmt.Fprintf(os.Stdout, "%s\n", outstring)
	}
}