// serialize gets called by the respective handler methods to perform the serialization in the requested output representation func serialize(latlong *cartconvert.PolarCoord, oformat string) (interface{}, error) { var serializestruct interface{} var err error switch oformat { case OFlatlongdeg: lat, long := cartconvert.LatLongToString(latlong, cartconvert.LLFdms) serializestruct = &LatLong{Lat: lat, Long: long, Fmt: cartconvert.LLFdms.String(), LatLongString: latlong.String()} case OFlatlongcomma: lat, long := cartconvert.LatLongToString(latlong, cartconvert.LLFdeg) serializestruct = &LatLong{Lat: lat, Long: long, Fmt: cartconvert.LLFdeg.String(), LatLongString: latlong.String()} case OFgeohash: serializestruct = &GeoHash{GeoHash: cartconvert.LatLongToGeoHash(latlong)} case OFUTM: utm := cartconvert.LatLongToUTM(latlong) serializestruct = &UTMCoord{UTMCoord: utm, UTMString: utm.String()} case OFBMN: var bmnval *bmn.BMNCoord bmnval, err = bmn.WGS84LatLongToBMN(latlong, bmn.BMNZoneDet) if err == nil { serializestruct = &BMN{BMNCoord: bmnval, BMNString: bmnval.String()} } case OFOSGB: var osgb36val *osgb36.OSGB36Coord osgb36val, err = osgb36.WGS84LatLongToOSGB36(latlong) if err == nil { serializestruct = &OSGB36{OSGB36Coord: osgb36val, OSGB36String: osgb36val.String()} } default: err = fmt.Errorf("Unsupported output format: '%s'", oformat) } return serializestruct, err }
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) } }