需要列出世界上所有国家的经纬度坐标 [英] Need a list of all countries in the world, with a longitude and latitude coordinate

查看:560
本文介绍了需要列出世界上所有国家的经纬度坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一份世界上所有国家的名单,一国经纬度坐标。



我有一个看一下GeoNames,但我可以看到的所有列表都包含列表中的国家和城市。我想我可以解析它并过滤出国家,但我希望避免这种情况。



基本上,我需要的是什么;

国家名称 - 经纬度坐标(国家的某个中心)

不需要说大陆的任何内容,但是我不介意这样做。



_L

解决方案

您可能有兴趣建立这些数据的两种网络服务: http://www.geonames.org/export/geonames-search.html

  • http://www.geonames.org/export/web-services.html#countryInfo



  • 您可以调用 http://ws.geonames.org/countryInfo 获取国家代码和国家名称的完整列表,a然后调用 http://ws.geonames.org/search?country=<code>&name=<name>&maxRows=1 得到似乎是该国中心点的经纬度。 (例如 http://ws.geonames.org/搜索?country = US& name = United%20States& maxRows = 1

    编辑

    这是一个C#脚本,您可以针对上面列出的服务运行。

    您可以按照 LinqPad

      void Main()
    {
    var countries = new List< Country>();
    XDocument xmlDoc = XDocument.Load(@http://ws.geonames.org/countryInfo);
    var countryList =(从xmlDoc.Descendants(country)中的c选择c);

    foreach(countryList中的var国家)
    {
    var countryPath = string.Format(http://ws.geonames.org/search?country={0}& ; name = {1}& maxRows = 1,country.Element(countryCode)。value,country.Element(countryName)。Value);
    var element =(从XDocument.Load(countryPath).decendants(geoname)中的e选择e).FirstOrDefault();

    countries.Add(新国家
    {
    Code = element.Element(countryCode)。Value,
    Name = element.Element(countryName) .value,
    Latitude = Convert.ToDecimal(element.Element(lat)。Value),
    Longitude = Convert.ToDecimal(element.Element(lng)。Value),
    Continent = country.Element(continentName)。Value
    });


    Console.WriteLine({0},{1},{2},{3},{4},Continent,Code,Name ,纬度,经度);

    foreach(国家/地区的国家/地区)
    {
    Console.WriteLine({0},{1},{2},{3},{4}, country.Continent,country.Code,country.Name,country.Latitude,country.Longitude);



    public class Country
    {
    public string Continent {get; set;}
    public string Name {get; set ;}
    public string Code {get; set;}
    public decimal Latitude {get; set;}
    public decimal Longitude {get; set;}
    }


    I need a list of all the countries in the world, with one lat/long coordinate for the country.

    I had a look at GeoNames, but all I can seem to find are lists that have the countries as well as cities in the same list. I guess I can just parse it and filter out the countries, but I was hoping to avoid that.

    Basically, what I need;

    Country name - Lat/long coordinate (some sort of center of the country)

    It doesn't need to say anything about continent, but I wouldn't mind if it did.

    _L

    解决方案

    Two web services that may be of interest to you to build this data:

    You could call http://ws.geonames.org/countryInfo to get the full list of country codes and country names, and then call http://ws.geonames.org/search?country=<code>&name=<name>&maxRows=1 on each to get what appears to be the lat/long of the center-point of the country. (ex. http://ws.geonames.org/search?country=US&name=United%20States&maxRows=1 )

    EDIT

    Here is a C# script that you can run against the services listed above.
    You can run this as is in LinqPad.

    void Main()
    {
        var countries = new List<Country>();
        XDocument xmlDoc = XDocument.Load(@"http://ws.geonames.org/countryInfo");
        var countryList = (from c in xmlDoc.Descendants("country") select c);
    
        foreach (var country in countryList) 
        {
            var countryPath = string.Format("http://ws.geonames.org/search?country={0}&name={1}&maxRows=1", country.Element("countryCode").Value, country.Element("countryName").Value);
            var element = (from e in XDocument.Load(countryPath).Descendants("geoname") select e).FirstOrDefault();
    
            countries.Add(new Country
            { 
                Code = element.Element("countryCode").Value, 
                Name = element.Element("countryName").Value, 
                Latitude = Convert.ToDecimal(element.Element("lat").Value), 
                Longitude =  Convert.ToDecimal(element.Element("lng").Value),
                Continent = country.Element("continentName").Value
            });
        }
    
        Console.WriteLine("{0},{1},{2},{3},{4}","Continent", "Code", "Name", "Latitude", "Longitude");  
    
        foreach (var country in countries) 
        {
            Console.WriteLine("{0},{1},{2},{3},{4}",country.Continent, country.Code, country.Name, country.Latitude, country.Longitude);    
        }
    }
    
    public class Country
    { 
        public string Continent {get;set;} 
        public string Name { get;set;} 
        public string Code { get;set;} 
        public decimal Latitude { get;set;} 
        public decimal Longitude { get;set;} 
    }
    

    这篇关于需要列出世界上所有国家的经纬度坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆