import json import math def calculate_heading(lat1, lon1, lat2, lon2): """ 计算两点之间的航向角 返回角度值(0-360度) """ # 将经纬度从度转换为弧度 lat1 = math.radians(lat1) lon1 = math.radians(lon1) lat2 = math.radians(lat2) lon2 = math.radians(lon2) # 计算经度差 dLon = lon2 - lon1 # 使用反正切函数计算航向角 y = math.sin(dLon) * math.cos(lat2) x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dLon) bearing = math.atan2(y, x) # 将弧度转换为度数 bearing = math.degrees(bearing) # 确保结果在0-360度范围内 bearing = (bearing + 360) % 360 return bearing def process_gps_data_with_headings(input_data): """ 处理GPS数据,为每个点添加航向角 """ processed_data = [] points = input_data["rs"] for i in range(len(points)): lon, lat = points[i][0], points[i][1] # 数据中是[lon, lat]格式 if i == len(points) - 1: # 最后一个点没有下一个点,使用前一个点的方向 if i > 0: prev_lon, prev_lat = points[i-1][0], points[i-1][1] heading = calculate_heading(prev_lat, prev_lon, lat, lon) else: # 只有一个点的情况,设为0度 heading = 0 else: # 计算到下一个点的航向角 next_lon, next_lat = points[i+1][0], points[i+1][1] heading = calculate_heading(lat, lon, next_lat, next_lon) processed_data.append([lon, lat, heading]) # 创建新的数据结构 result = {"rs": processed_data} return result # 新的GPS数据(从Car_fangzhen_call.txt获取) new_original_data = { "rs": [ [116.5024625,39.8090317],[116.50246452571352,39.809032676237166],[116.50246655142706,39.80903365247434],[116.5024685771406,39.80903462871151],[116.50247060285415,39.80903560494868],[116.50247262856767,39.80903658118585],[116.50247465428123,39.809037557423025],[116.50247667999476,39.80903853366019],[116.5024787057083,39.80903950989737],[116.50248073142184,39.809040486134535],[116.50248275713537,39.80904146237171],[116.50248478284891,39.80904243860888],[116.50248554937339,39.80904280015562],[116.50248625256704,39.8090431183115],[116.50248696285315,39.809043426953984],[116.50248768001542,39.80904372598901],[116.50248840383534,39.80904401532554],[116.50248913409246,39.80904429487541],[116.50248987056432,39.80904456455347],[116.50249061302658,39.809044824277564],[116.50249136125309,39.80904507396862],[116.50249211501594,39.809045313550534],[116.5024928740855,39.809045542950344],[116.50249363823058,39.809045762098165],[116.50249440721838,39.80904597092725],[116.50249518081468,39.809046169374014],[116.50249595878384,39.80904635737795],[116.50249674088887,39.80904653488182],[116.50249752689155,39.80904670183156],[116.50249831655243,39.809046858176316],[116.50249910963099,39.80904700386845],[116.50249990588564,39.8090471388636],[116.50250070507383,39.80904726312063],[116.50250150695214,39.8090473766017],[116.50250231127629,39.80904747927224],[116.50250311780128,39.809047571100976],[116.50250392628145,39.80904765205992],[116.5025047364705,39.80904772212444],[116.50250554812165,39.80904778127317],[116.50250636098767,39.8090478294881],[116.50250717482095,39.809047866754554],[116.50250798937358,39.80904789306116],[116.50250880439745,39.80904790839994],[116.50250961964429,39.809047912766175],[116.50251043486577,39.80904790615856],[116.50251124981355,39.809047888579094],[116.50251206423943,39.80904786003317],[116.50251287789527,39.80904782052943],[116.50251369053326,39.80904777007994],[116.50251450190586,39.80904770870008],[116.5025153117659,39.80904763640851],[116.50251611986671,39.80904755322727],[116.50251692596213,39.8090474591817],[116.5025177298066,39.809047354300446],[116.50251853115527,39.80904723861545],[116.50251932976404,39.80904711216196],[116.50252012538965,39.80904697497849],[116.50252091778974,39.80904682710682],[116.50252170672294,39.80904666859201],[116.50252249194894,39.80904649948232],[116.50252327322852,39.80904631982929],[116.50252405032373,39.80904612968762],[116.50252482299786,39.809045929115264],[116.50252559101551,39.809045718173266],[116.50252635414277,39.80904549692593],[116.50252711214716,39.80904526544063],[116.50252786479781,39.80904502378788],[116.50252861186543,39.80904477204128],[116.50252935312245,39.80904451027753],[116.50253008834312,39.80904423857637],[116.50253081730344,39.80904395702054],[116.50253153978137,39.80904366569581],[116.50253225555686,39.80904336469095],[116.50253296441184,39.80904305409761],[116.50253366613042,39.80904273401042],[116.50253436049884,39.80904240452689],[116.50253504730557,39.80904206574736],[116.50253572634142,39.809041717775045],[116.50253639739954,39.80904136071593],[116.50253706027554,39.80904099467878],[116.50253771476747,39.8090406197751],[116.50253836067598,39.809040236119095],[116.50253899780434,39.80903984382761],[116.50253962595843,39.80903944302016],[116.50254024494696,39.809039033818834],[116.50254085458133,39.809038616348246],[116.50254145467586,39.80903819073561],[116.50254204504778,39.80903775711055],[116.50254262551721,39.80903731560515],[116.50254319590738,39.80903686635389],[116.5025437560445,39.80903640949364],[116.502544305758,39.809035945163544],[116.50254484488038,39.80903547350505],[116.50254537324744,39.809034994661815],[116.50254589069823,39.80903450877972],[116.50254639707514,39.80903401600677],[116.5025468922239,39.80903351649306],[116.50254873251244,39.80903121870854],[116.50255048525985,39.80902903906369],[116.50255215519805,39.809026971172585],[116.50255374705901,39.80902500864927],[116.50255526557463,39.809023145107844],[116.50255671547697,39.80902137416238],[116.50255810149788,39.80901968942692],[116.50255942836942,39.80901808451557],[116.5025607008234,39.80901655304239],[116.50256192359191,39.80901508862146],[116.50256310140682,39.80901368486684],[116.50256423900012,39.8090123353926],[116.50256534110378,39.80901103381284],[116.50256641244967,39.80900977374158],[116.50256745776986,39.809008548792946],[116.50256848179617,39.80900735258098],[116.50256948926067,39.80900617871975],[116.50257048489526,39.80900502082334],[116.50257147343187,39.809003872505826],[116.50257245960252,39.80900272738129],[116.50257344813913,39.809001579063775],[116.50257444377361,39.80900042116737],[116.50257545123799,39.80899924730615],[116.50257647526416,39.80899805109418],[116.50257752058411,39.80899682614553],[116.50257859192976,39.80899556607427],[116.5025796940331,39.808994264494494],[116.50258083162609,39.808992915020255],[116.50258200944063,39.80899151126563],[116.50258323220868,39.808990046844684],[116.50258450466227,39.808988515371496],[116.50258583153325,39.80898691046014],[116.50258721755367,39.80898522572469],[116.5025886674554,39.80898345477921],[116.50259018597043,39.808981591237774],[116.5025917778307,39.808979628714454],[116.50259344776819,39.80897756082334],[116.50259520051483,39.80897538117847],[116.50259704080258,39.80897308339394], [116.50260652829927, 39.80896138259575], [116.50261595656403, 39.80894963426086], [116.50262538482876, 39.80893788592398], [116.50263481309352, 39.80892613758509], [116.50264424135828, 39.80891438924419], [116.50265366962303, 39.80890264090128], [116.50266309788779, 39.80889089255635], [116.50267252615251, 39.808879144209435], [116.50268195441728, 39.808867395860496], [116.50269138268203, 39.80885564750955], [116.50270081094675, 39.8088438991566], [116.50271023921152, 39.80883215080165], [116.50271966747628, 39.80882040244469], [116.50272909574102, 39.8088086540857], [116.50273852400579, 39.80879690572473], [116.50274795227055, 39.80878515736174], [116.50275738053527, 39.808773408996736], [116.50276680880005, 39.80876166062974], [116.5027762370648, 39.80874991226073], [116.50278566532953, 39.80873816388972], [116.50279420230066, 39.80872752614001], [116.50280353151834, 39.80871573236971], [116.50281048428627, 39.80870303338525], [116.50281405862891, 39.80868953352494], [116.50281404037212, 39.80867575730624], [116.50281046861977, 39.80866225701582], [116.50280343493115, 39.808649585669684], [116.50279324712442, 39.80863824913313], [116.50278027954208, 39.80862873543999], [116.50276508406697, 39.80862142721356], [116.50274825941413, 39.80861675684903], [116.5027303799696, 39.808616110118535], [116.50271293867053, 39.808619088363244], [116.50269720864796, 39.80862564603595], [116.50268447677053, 39.80863527937748], [116.50267341280619, 39.80864615002302], [116.50266212329731, 39.80865688638265], [116.5026508337884, 39.80866762274061], [116.50263954427948, 39.80867835909688], [116.50262525253801, 39.80869195058276], [116.50262524257603, 39.808691950612655], [116.50261580612782, 39.80870369511379], [116.50260636967965, 39.80871543961292], [116.50259693323143, 39.80872718411004], [116.50258749678324, 39.80873892860516], [116.50257806033504, 39.808750673098274], [116.50256862388684, 39.80876241758937], [116.50255918743866, 39.808774162078464], [116.50254974864124, 39.808785905451415], [116.50254030866101, 39.8087976482614], [116.50253086868078, 39.80880939106937], [116.50252142870058, 39.80882113387535], [116.50251198872037, 39.8088328766793], [116.50250254874015, 39.80884461948126], [116.50249310875992, 39.808856362281205], [116.50248366877972, 39.80886810507916], [116.5024742287995, 39.8088798478751], [116.5024647888193, 39.808891590669035], [116.50245534883908, 39.80890333346096], [116.50244590885885, 39.80891507625088], [116.50243648704945, 39.80892682764632], [116.50242706678878, 39.80893857977338], [116.50241764652812, 39.80895033189842], [116.50240822626745, 39.808962084021466], [116.50239880600678, 39.808973836142506], [116.50238938574607, 39.80898558826153], [116.5023799654854, 39.808997340378546], [116.50237054522474, 39.809009092493554], [116.50236112496407, 39.80902084460654], [116.5023518081128, 39.80903246771061], [116.50234241407298, 39.80904423219872], [116.50233302003318, 39.80905599668481], [116.50232362599334, 39.8090677611689], [116.50231410317872, 39.80907946408927], [116.50230451214804, 39.80909113439654], [116.50229492111734, 39.80910280470181], [116.50228571333837, 39.809114655344956], [116.50227653686962, 39.80912652071914], [116.50226736040084, 39.80913838609128], [116.5022581839321, 39.80915025146137], [116.50224999247808, 39.809160843184586], [116.50224042543937, 39.80917252509297], [116.50223085840065, 39.80918420699937], [116.50222129136195, 39.80919588890379], [116.50221172432323, 39.809207570806215], [116.50220215728451, 39.809219252706654], [116.50219259024581, 39.80923093460512], [116.5021830232071, 39.809242616501585], [116.50217345616836, 39.80925429839608], [116.50216388912965, 39.80926598028858], [116.50215432209092, 39.80927766217909], [116.50214475505221, 39.80928934406762], [116.5021351880135, 39.80930102595418], [116.50212562097478, 39.809312707838735], [116.5021161682309, 39.80932444445018], [116.50210672752434, 39.80933618682361], [116.50209728681781, 39.80934792919502], [116.50208784611127, 39.80935967156444], [116.50207840540475, 39.809371413931835], [116.50206896469822, 39.80938315629724], [116.50205952399168, 39.809394898660635], [116.50205008328514, 39.809406641022015], [116.5020406425786, 39.80941838338139], [116.50203120187207, 39.809430125738764], [116.50202176116555, 39.80944186809414], [116.50201232045903, 39.809453610447505], [116.50200287975248, 39.80946535279886], [116.50199343904592, 39.80947709514822], [116.50198399833941, 39.809488837495564], [116.50197455763286, 39.80950057984091], [116.50196511692633, 39.80951232218425], [116.50195568818478, 39.80952407018145], [116.50194629734833, 39.80953583609448], [116.50193690651183, 39.80954760200551], [116.50192751567536, 39.80955936791453], [116.50191812483887, 39.80957113382152], [116.5019087340024, 39.8095828997265], [116.50189934316592, 39.80959466562948], [116.50188995232944, 39.80960643153043], [116.50188056149298, 39.80961819742937], [116.50187117065649, 39.80962996332631], [116.50186177982, 39.80964172922121], [116.50185238898351, 39.80965349511412], [116.50184299814707, 39.80966526100501], [116.50183360731057, 39.80967702689388], [116.50182421647412, 39.80968879278074], [116.50181482563762, 39.809700558665575], [116.50180543480116, 39.809712324548414], [116.50179604396466, 39.809724090429235], [116.5017866531282, 39.80973585630804], [116.50177726229172, 39.80974762218483], [116.50176603303983, 39.809761691428776], [116.5017559315272, 39.80977310463863], [116.50174606013915, 39.80978461575066], [116.501745399369, 39.80979802876061], [116.50176111803627, 39.80981199004545], [116.5017794095362, 39.80982207596424], [116.50179458916823, 39.8098294587058], [116.50180976712517, 39.8098368434775], [116.50182494052174, 39.80984423377744], [116.50183986641409, 39.80985190936817], [116.50186093375147, 39.80986323461995], [116.50187602871003, 39.80987071910237], [116.50189112366859, 39.80987820358399], [116.50190621862716, 39.80988568806478], [116.50192135787326, 39.80989311925764], [116.50193654901727, 39.80990048800575], [116.5019517411002, 39.80990785560883], [116.5019669480251, 39.80991520512269], [116.5019889433169, 39.80992583545604], [116.50200411254258, 39.80993323079723], [116.50201928176824, 39.80994062613762], [116.5020344549998, 39.80994801662329], [116.50204936866068, 39.809955271625505] ] } # 处理新的数据并添加航向角 processed_data = process_gps_data_with_headings(new_original_data) # 将结果保存为JSON格式 json_output = json.dumps(processed_data, indent=2, ensure_ascii=False) print("处理完成!以下是包含航向角的JSON数据:") print(json_output) # 如果需要保存到文件 with open('gps_data_with_headings.json', 'w', encoding='utf-8') as f: f.write(json_output) print("\n数据已保存到 gps_data_with_headings.json 文件中")