*- 設(shè)定工作目錄 cd'~/Desktop/使用Stata進行地理編碼:高德與百度接口/' import excel using '金融機構(gòu)網(wǎng)點數(shù)據(jù).xlsx', clear first foreach i of varlist _all { capformat`i' %10s }
高德地圖的 city 參數(shù)既可以是城市名稱也可以是城市區(qū)劃代碼,百度地圖的 city 參數(shù)只能是城市名稱,所以我們需要準備兩個 city 變量。
gen address = 機構(gòu)地址 gen citygd = 地址代碼 gen citybd = 機構(gòu)所在地 format address city* %20s replace citybd = ustrregexs(1) if ustrregexm(citybd, '-(.*)') keep id 機構(gòu)名稱 address citybd citygd
*- 準備空變量 clear gen str100 formatted_address = '' gen str100 province = '' gen str100 citycode = '' gen str100 city = '' gen str100 district = '' gen str100 adcode = '' gen str100 location = '' gen str100 level = '' *- 處理 json 數(shù)據(jù) insheetjson formatted_address province citycode city district adcode location level using 'temp.json', columns('formatted_address''province''citycode''city''district''adcode''location''level') tableselector('geocodes') compress
clear gen str100 formatted_address = '' gen str100 province = '' gen str100 city = '' gen str100 district = '' gen str100 location = '' gen str100 level = '' gen str100 id = '' *- 循環(huán) json1 文件夾下的所有文件 local files: dir'json1' files '*.json' localn: word count`files'
foreach i in`files' { insheetjson formatted_address province city /// district location level using 'json1/`i'', /// columns('formatted_address''province'/// 'city''district''location''level') /// tableselector('geocodes') offset(`=_N') replace id = '`i''ifmi(id) }
compress
foreach i of varlist _all { capformat`i' %10s }
order id replace id = subinstr(id, '.json', '', .) destring id, replace
和原始數(shù)據(jù)匹配:
mergem:1 id using dfsim keepif _m == 3 drop _m order id 機構(gòu)名稱 address citygd citybd
*- 根據(jù)經(jīng)緯度可以判斷所處的省市區(qū)縣,所以這里先刪去省市區(qū)縣信息 keep id location save'解析結(jié)果1', replace
沒有成功的:
use dfsim, clear merge 1:1 id using 解析結(jié)果1 keepif _m == 1 drop _m
使用百度地圖地理編碼接口解析
剩下的這些使用百度地圖地理編碼接口解析:
use dfsim, clear merge 1:1 id using 解析結(jié)果1 keepif _m == 1 drop _m replace address = address + 機構(gòu)名稱 drop location compress save dfsim2, replace
use dfsim2, clear capmkdir'json2' forvalg = 1/`=_N' { percentencode `=address[`g']' local add = '`r(percentencode)'' percentencode `=citybd' local cty = '`r(percentencode)'' local url = 'https://api.map.baidu.com/geocoding/v3/?address=`add'&output=json&ak=h4jN43d3ajnnXGzBHmIowERWwM2o6PIe&ret_coordtype=gcj02ll&city=`cty'' copy'`url'''json2/`=id[`g']'.json' }
*- 看起來都成功了 clear gen str100 lng = '' gen str100 lat = '' gen str100 precise = '' gen str100 confidence = '' gen str100 comprehension = '' gen str100 level = '' gen str100 id = ''
*- 循環(huán) json2 文件夾下的所有文件 local files: dir'json2' files '*.json' localn: word count`files' foreach i in`files' { insheetjson lng lat precise confidence /// comprehension level /// using 'json2/`i'', table('result') /// columns('location:lng''location:lat''precise'/// 'confidence''comprehension''level') offset(`=_N') replace id = '`i''ifmi(id) } compress order id replace id = subinstr(id, '.json', '', .) destring id, replace gen location = lng + ',' + lat
*- 這里也可以根據(jù)數(shù)據(jù)中的變量判斷解析情況。 keep id location save'解析結(jié)果2', replace
合并所有的成功結(jié)果
use 解析結(jié)果1, clear append using 解析結(jié)果2 gsort id unique id split location, parse(,) drop location ren location1 經(jīng)度 ren location2 緯度 destring 經(jīng)度 緯度, replace
GCJ02 坐標轉(zhuǎn)換成 WGS84 坐標
附件中的 gcj02towgs84.ado 命令可以直接完成這個操作:
gcj02towgs84, lon(經(jīng)度) lat(緯度) save finalresult, replace
根據(jù)經(jīng)緯度判斷所處的省市區(qū)縣
之前介紹過這個操作,不過不是 Stata 完成的,最近發(fā)現(xiàn)原來 Stata 中也有這種命令:geoinpoly。
安裝:
ssc install geoinpoly
把 shp 文件轉(zhuǎn)換成 dta 文件:
shp2dta using 2021行政區(qū)劃/縣.shp, database(county_db) coordinates(county_coord) genid(ID) replace
然后就可以使用該命令進行經(jīng)緯度判斷省市區(qū)縣的操作了:
use finalresult, clear geoinpoly 緯度_WGS84 經(jīng)度_WGS84 using county_coord.dta ren _ID ID mergem:1 ID using county_db keepif _m == 3 drop _m foreach i of varlist _all { capformat`i' %10s } *- 這樣我們就完成了判斷所處省市區(qū)縣的操作 save finalresult2, replace
效率非常高!
直播信息
為了讓大家更好的理解本文內(nèi)容,歡迎各位培訓(xùn)班會員參加明晚 8 點的直播課:「使用 Stata 進行地理編碼:地址解析經(jīng)緯度、坐標轉(zhuǎn)換 & 根據(jù)經(jīng)緯度判斷所處的省市區(qū)縣」