官术网_书友最值得收藏!

Sort the countries by area size

You programmed three functions so far; now, let's add another one to our list by converting the code that generated a list of country names to a function and add this function to world_areas.py, as follows:

def get_country_names(datasource):
    """Returns a list of country names."""
    layer = datasource.GetLayerByIndex(0)
    country_names = []
    layer.ResetReading()
    for feature in layer:
        country_names.append(feature.GetFieldAsString(4))
    return country_names

Now, we have four functions, which are:

  • open_shapefile
  • transform_geometries
  • calculate_areas
  • get_country_names

All these functions return iterables, with each item sharing the same index on all of them, thus making it easy to combine the information.

So, let's take advantage of this feature to sort the countries by area size and return a list of the five biggest countries and their areas. For this, add another function, as follows:

def get_biggest_countries(countries, areas, elements=5):
    """Returns a list of n countries sorted by area size."""    
    countries_list = [list(country) 
                      for country in zip(areas, countries)]

    sorted_countries = sorted(countries_list, 
                              key=itemgetter(0), reverse=True) 
    return sorted_countries[:5]

In the first line, the two lists are zipped together, producing a list of country-area pairs. Then, we used the Python list's sorted method, but as we don't want the lists to be sorted by both values, we will define the key for sorting. Finally, the list is sliced, returning only the desired number of values.

  1. In order to run this code, you need to import the itemgetter function and put it at the beginning of the code but after from __future__ imports, as follows:
    from operator import itemgetter
  2. Now, edit the testing part of your code to look similar to the following:
    datasource = open_shapefile("../data/world_borders_simple.shp")
    transformed_geoms = transform_geometries(datasource, 4326, 3395)
    country_names = get_country_names(datasource)
    country_areas = calculate_areas(transformed_geoms)
    biggest_countries = get_biggest_countries(country_names, 
                                              country_areas)
    for item in biggest_countries:
        print("{}\t{}".format(item[0], item[1])) 
  3. Now, run the code and take a look at the results, as follows:
    Opening ../data/world_borders_simple.shp
    Number of features: 246
    82820725.1423 Russia
    51163710.3726 Canada
    35224817.514 Greenland
    21674429.8403 United States
    14851905.8596 China
    
    Process finished with exit code 0
主站蜘蛛池模板: 嘉义县| 福安市| 衡山县| 通化县| 临高县| 遂昌县| 湄潭县| 安宁市| 萝北县| 老河口市| 阿荣旗| 株洲县| 峨眉山市| 赣榆县| 饶阳县| 武清区| 方山县| 洞头县| 霍山县| 靖江市| 宁乡县| 乐至县| 牟定县| 安泽县| 微博| 鄂托克前旗| 哈尔滨市| 肥城市| 浙江省| 手机| 平昌县| 乐平市| 克什克腾旗| 桂林市| 普陀区| 依兰县| 岢岚县| 勐海县| 嘉荫县| 盐山县| 三门峡市|