From the Baidu Map API interface batch to obtain the latitude and longitude of the location

Source: Internet
Author: User

Today, my colleague wants to make a plan to get the latitude and longitude of more than 2000 addresses in Excel. Ask me there is no way, just my time to learn Python, think about it, think it can. So I wrote one of the following code. Just started to think that almost two hours can be done, the results cost more than half a day, Khan ... Mainly in the card from the Baidu Map API gets the coordinates are always wrong. Later on-line data to understand that the original from the Baidu API is Mercator coordinates, and the actual use of WGS84 coordinates.

1 #!/usr/bin/python2 #Coding:utf-83  4 Importxlrd5 ImportXLWT6 ImportRequests7 ImportUrllib8 ImportMath9 ImportReTen   OnePattern_x=re.compile (R'"x":(". +?")') APattern_y=re.compile (R'"y":(". +?")') -   - defmercator2wgs84 (Mercator): the     #Key1=mercator.keys () [0] -     #Key2=mercator.keys () [1] -point_x=Mercator[0] -Point_y=mercator[1] +x=point_x/20037508.3427892*180 -y=point_y/20037508.3427892*180 +y=180/math.pi* (2*math.atan (Math.exp (y*math.pi/180))-MATH.PI/2) A     return(x, y) at   - defget_mercator (addr): -Quote_addr=urllib.quote (Addr.encode ('UTF8')) -City=urllib.quote (U'Qiqihar Dragon'. Encode ('UTF8')) -Province=urllib.quote (U'Heilongjiang Province'. Encode ('UTF8')) -     ifQuote_addr.startswith (city)orQuote_addr.startswith (province): in         Pass -     Else: toquote_addr=city+quote_addr +S=urllib.quote (U'Beijing'. Encode ('UTF8')) -Api_addr="http://api.map.baidu.com/?qt=gc&wd=%s&cn=%s&ie=utf-8&oue=1&fromproduct=jsapi&res= api&callback=bmap._rd._cbk62300"%(quote_addr the , s) *req=requests.get (API_ADDR) $Content=req.contentPanax Notoginsengx=Re.findall (pattern_x,content) -y=Re.findall (pattern_y,content) the     ifx: +x=X[0] Ay=Y[0] theX=x[1:-1] +Y=y[1:-1] -x=float (x) $y=float (y) $location=(x, y) -     Else: -location=() the     return Location -  Wuyi defrun (): theData=xlrd.open_workbook ('Book2.xls') -rtable=data.sheets () [0] WuNrows=rtable.nrows -values=rtable.col_values (0) About       $workbook=XLWT. Workbook () -Wtable=workbook.add_sheet ('Data', cell_overwrite_ok=True) -row=0 -      forValueinchvalues: AMercator=Get_mercator (value) +         ifMercator: thewgs=mercator2wgs84 (Mercator) -         Else: $Wgs= ('NotFound','NotFound') the         Print "%s,%s,%s"% (value,wgs[0],wgs[1]) the wtable.write (Row,0,value) theWtable.write (row,1, Wgs[0]) theWtable.write (row,2,wgs[1]) -Row=row+1 in   theWorkbook.save ('Data.xls') the   About if __name__=='__main__': theRun ()

From the Baidu Map API interface batch to obtain the latitude and longitude of the location

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.