当前位置:网站首页>Provincial and urban level three coordinate boundary data CSV to JSON

Provincial and urban level three coordinate boundary data CSV to JSON

2022-07-07 00:56:00 Little shopkeeper selling sesame oil

  Sometimes when we draw the following map , Frame a city , Or the area of the county :

At this time, when coding and drawing , We need corresponding administrative districts json Format boundary coordinate set .

My last article wrote ,  Use python, utilize request Tools , Open from Gaode map API Obtain the longitude and latitude coordinate sets of the boundaries of various administrative regions in China .

Obtain the latitude coordinates of all administrative regions in China in batches ( To the county level )_ The blog of the little shopkeeper who sells sesame oil -CSDN Blog This article USES the python, utilize request Tools , Open from Gaode map API Obtain the longitude and latitude coordinate sets of the boundaries of various administrative regions in China . Administrative districts are most subdivided into county and district levels .https://blog.csdn.net/qq_58832911/article/details/125617715 But some areas with the same level and the same name , Will use the same boundary . This is a little bug.

This article is about , Public coordinate data set conversion is used Json Format , Implement access json Format boundary coordinate set .

csv Data sources are freely available :

newest 2022 The data of provincial, urban, county and township administrative divisions in can be imported mysql sql server The database can be linked at three levels and four levels , Coordinate boundary range vector data support shp geojson arcgis arcmap qgis mapinfo gis Geofencing - AreaCity-JsSpider-StatsGovhttps://xiangyuecn.gitee.io/areacity-jsspider-statsgov/

 

  You can also download my online disk :

link :https://pan.baidu.com/s/1AU7sFHoBQBtWT3lMBZAZHg 
Extraction code :ucv9 

csv What the data looks like :

 

  Purpose : Convert to this format json Format

 

import pandas as pd
import json
import re



#  read  ok_geo.csv csv file 
def read_csv(file_name):
    df = pd.read_csv(file_name, encoding='utf-8')
    #  Create a national dictionary 
    country_dict = {"name": "China", "border": None, "area": []}

    #  For experiments n Control the number of cycles 
    n = 0
    for index, row in df.iterrows():
        #  Determine whether it is a primary address 
        if row["deep"] == 0:
            #  Create a provincial dictionary 
            province_dict = {"name": None, "center": None, "border": None, "area": []}
            #  Assign a province dictionary 
            province_dict["name"] = row["name"]
            #  Get the provincial coordinate center 
            province_dict["center"] = [float(i) for i in row["geo"].split(" ")]
            # print(province_dict["center"])
            province_border = row["polygon"]
            #  Press "," Division , Then divide according to the space , Convert floating point type 
            province_border = [[float(i) for i in re.split(" |;", j)] for j in province_border.split(",")]
            province_dict["border"] = province_border
            #  Add the provincial dictionary to the national dictionary area in 
            country_dict["area"].append(province_dict)

        elif row["deep"] == 1:
            #  Create a city dictionary 
            city_dict = {"name": None, "center": None, "border": None, "countyArea": []}
            #  Assignment City dictionary 
            city_dict["name"] = row["name"]
            try:
                #  Get the city coordinate center 
                city_dict["center"] = [float(i) for i in row["geo"].split(" ")]
            except:
                city_dict["center"] = None
            #  Get the city boundary 
            try:
                city_border = row["polygon"]
                #  Press "," Division , Then divide according to the space , Convert floating point type 
                city_border = [[float(i) for i in re.split(' |;', j)] for j in city_border.split(",")]
            except:
                city_border = None
            city_dict["border"] = city_border

            #  Judge which province it belongs to 
            belong_to_p = row["ext_path"].split(" ")[0]
            # print(belong_to_p)
            #  Look up the province dictionary in the list of provinces in the national dictionary name Property value and belong_to_p The index of the list of dictionaries in the same province 
            for i, x in enumerate(country_dict["area"]):
                if x["name"] == belong_to_p:
                    #  Add city dictionary to provincial dictionary area in 
                    p_index = i
                    break
            # print(p_index)
            country_dict["area"][p_index]["area"].append(city_dict)

        else:
            #  Create a county dictionary 
            district_dict = {"name": None, "center": None, "border": None}
            #  Assign County dictionary 
            district_dict["name"] = row["name"]
            #  Get the county coordinate center 
            try:
                district_dict["center"] = [float(i) for i in row["geo"].split(" ")]
            except:
                district_dict["center"] = []
            #  Get boundary coordinates 
            try:
                district_border = row["polygon"]
                #  Press "," Division , Then divide according to the space , Convert floating point type 
                district_border = [[float(i) for i in re.split(' |;', j)] for j in district_border.split(",")]
            except:
                district_border = []
            district_dict["border"] = district_border
            #  Judge which province it belongs to 
            belong_to_p, belong_to_c= row["ext_path"].split(" ")[0], row["ext_path"].split(" ")[1]
            # print(belong_to_p)
            #  Look up the province dictionary in the list of provinces in the national dictionary name Property value and belong_to_p The index of the list of dictionaries in the same province 
            for i, x in enumerate(country_dict["area"]):
                if x["name"] == belong_to_p:
                    #  Add the county dictionary to the city dictionary countyArea in 
                    p_index = i
                    break
            # print(p_index)
            #  Judge which city it belongs to 
            for i, x in enumerate(country_dict["area"][p_index]["area"]):
                if x["name"] == belong_to_c:
                    c_index = i
                    break
            # print(c_index)
            country_dict["area"][p_index]["area"][c_index]["countyArea"].append(district_dict)


        # print(country_dict)
        print(" The second " + str(n) + " Secondary cycle ," + " The processing address is :" + row["ext_path"])

        n += 1
        if n==3:
            break

    # print(country_dict)
    #  write in json file 
    with open('demo.json', 'w', encoding='utf-8') as f:
        json.dump(country_dict, f, ensure_ascii=False)


if __name__ == "__main__":
    read_csv('ok_geo.csv')
    pass

result :

json Download address of the file .( But I hope you can run the code by yourself )

  link :https://pan.baidu.com/s/13RDBSmI0RSQHXllRr3Ke3Q 
Extraction code :homc 
 

Only for communication and discussion . Not for commercial purposes .

原网站

版权声明
本文为[Little shopkeeper selling sesame oil]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061707199575.html