2012-11-15 7 views
15

में देशों को भरें हाय मैं कुछ निश्चित देशों में भरे कुछ देशों के साथ अजगर बेसमैप का उपयोग करके एक नक्शा प्लॉट करने की कोशिश कर रहा हूं।पाइथन बेसमैप

क्या वहां कोई त्वरित और आसान समाधान है ??

+2

शायद उपयोगी है: http: //www.geophysique। हो/2011/01/27/matplotlib-basemap-tutorial-07-shapefiles-unleached/ – unutbu

+0

मैं इसे मदद करता हूं: http://matplotlib.1069221.n5.nabble.com/How-to-draw-a- विशिष्ट- देश-दर-बेसमैप-टीडी 15744.html –

+2

उन टिप्पणियों के लिए धन्यवाद, जहां वे सबसे उपयोगी हैं। मुझे मुफ्त देश डेटा के साथ एक साइट भी मिली, जो मैं चाहता था: [http://www.naturalearthdata.com/](http://www.naturalearthdata.com/) –

उत्तर

12

जैसा कि @unutbu द्वारा पहले ही कहा जा चुका है, थॉमस 'here ठीक है जो आप के बाद हैं।

import cartopy.crs as ccrs 
import matplotlib.pyplot as plt 
import cartopy.io.shapereader as shpreader 
import itertools 
import numpy as np 

shapename = 'admin_0_countries' 
countries_shp = shpreader.natural_earth(resolution='110m', 
             category='cultural', name=shapename) 

# some nice "earthy" colors 
earth_colors = np.array([(199, 233, 192), 
           (161, 217, 155), 
           (116, 196, 118), 
           (65, 171, 93), 
           (35, 139, 69), 
           ])/255. 
earth_colors = itertools.cycle(earth_colors) 



ax = plt.axes(projection=ccrs.PlateCarree()) 
for country in shpreader.Reader(countries_shp).records(): 
    print country.attributes['name_long'], earth_colors.next() 
    ax.add_geometries(country.geometry, ccrs.PlateCarree(), 
         facecolor=earth_colors.next(), 
         label=country.attributes['name_long']) 

plt.show() 

output

+3

कृपया ध्यान दें कि आपको इस साइट पर, उत्तर के आवश्यक हिस्सों को पोस्ट करना चाहिए, या आपके पोस्ट जोखिमों को हटाया जाना चाहिए [एफएक्यू देखें जहां यह उन उत्तरों का उल्लेख करता है जो 'लिंक से काफी अधिक' हैं।] (Http: // stackoverflow .com/faq # हटाना) यदि आप चाहें तो लिंक अभी भी शामिल कर सकते हैं, लेकिन केवल 'संदर्भ' के रूप में। लिंक को लिंक के बिना जवाब स्वयं ही खड़ा होना चाहिए। – Taryn

+1

धन्यवाद @bluefeet - मैं देख सकता हूं कि ऐसा क्यों होगा। मैंने कुछ नई जानकारी देने के लिए उत्तर अपडेट किया है (मूल लिंक को डुप्लिकेट किए बिना, जिस पर मेरा कॉपीराइट नहीं है)। चीयर्स, – pelson

+0

कॉलिंग shpreader.natural_earth मुझे एक HTTP 404 त्रुटि नहीं मिली है, यह स्पष्ट रूप से इसे डाउनलोड करने का प्रयास करता है? – Leo

9

pelson से जवाब से प्रेरित होकर, मैं पोस्ट:

आप Cartopy, इसी कोड (v0.7 में) के साथ ऐसा करने http://scitools.org.uk/cartopy/docs/latest/tutorials/using_the_shapereader.html थोड़ा से अनुकूलित किया जा सकता इच्छुक हों मेरे पास समाधान है। मैं इसे आपके पास छोड़ दूंगा जो सबसे अच्छा काम करता है, इसलिए मैं इस समय कोई जवाब स्वीकार नहीं करूंगा।);

#! /usr/bin/env python 

import sys 
import os 
from pylab import * 
from mpl_toolkits.basemap import Basemap 
import matplotlib as mp 

from shapelib import ShapeFile 
import dbflib 
from matplotlib.collections import LineCollection 
from matplotlib import cm 

def get_shapeData(shp,dbf): 
    for npoly in range(shp.info()[0]): 
    shpsegs = [] 
    shpinfo = [] 

    shp_object = shp.read_object(npoly) 
    verts = shp_object.vertices() 
    rings = len(verts) 
    for ring in range(rings): 
     if ring == 0: 
      shapedict = dbf.read_record(npoly) 
     name = shapedict["name_long"] 
     continent = shapedict["continent"] 
     lons, lats = zip(*verts[ring]) 
     if max(lons) > 721. or min(lons) < -721. or max(lats) > 91. or min(lats) < -91: 
      raise ValueError,msg 
     x, y = m(lons, lats) 
     shpsegs.append(zip(x,y)) 
     shapedict['RINGNUM'] = ring+1 
     shapedict['SHAPENUM'] = npoly+1 
     shpinfo.append(shapedict) 

    lines = LineCollection(shpsegs,antialiaseds=(1,)) 
    lines.set_facecolors(cm.jet(np.random.rand(1))) 
    lines.set_edgecolors('k') 
    lines.set_linewidth(0.3) 
    ax.add_collection(lines) 


if __name__=='__main__': 

    f=figure(figsize=(10,10)) 
    ax = plt.subplot(111) 
    m = Basemap(projection='merc',llcrnrlat=30,urcrnrlat=72,\ 
      llcrnrlon=-40,urcrnrlon=50,resolution='c') 
    m.drawcountries(linewidth=0.1,color='w') 

    sfile = 'ne_10m_admin_0_countries' 

    shp = ShapeFile(sfile) 
    dbf = dbflib.open(sfile) 
    get_shapeData(shp,dbf) 

    show() 
    sys.exit(0) 

यह यहाँ मेरी उदाहरण है कि कैसे अल्बानिया में सही रंग (बहुत ही सुंदर नहीं मुझे पता है) को भरने के लिए परिणाम

example for filling in countries in different colours

है।

#HACK for Albania 
    shpsegs = [] 
    shpinfo = [] 

    shp_object = shp.read_object(9) 
    verts = shp_object.vertices() 
    rings = len(verts) 
    for ring in range(rings): 
     if ring == 0: 
      shapedict = dbf.read_record(9) 
     name = shapedict["name_long"] 
     continent = shapedict["continent"] 
     lons, lats = zip(*verts[ring]) 
     if max(lons) > 721. or min(lons) < -721. or max(lats) > 91. or min(lats) < -91: 
      raise ValueError,msg 
     x, y = m(lons, lats) 
     shpsegs.append(zip(x,y)) 
     shapedict['RINGNUM'] = ring+1 
     shapedict['SHAPENUM'] = npoly+1 
     shpinfo.append(shapedict) 
    lines = LineCollection(shpsegs,antialiaseds=(1,)) 
    if name == 'Albania': 
    lines.set_facecolors('w') 
    lines.set_edgecolors('k') 
    lines.set_linewidth(0.3) 
    ax.add_collection(lines) 

यह महत्वपूर्ण है कि आप अन्य सभी आकारों के बाद ऐसा करते हैं। शायद आप इस कोड के कुछ हिस्सों से छुटकारा पा सकते हैं, लेकिन जैसा कि मैंने कहा था कि यह मेरे लिए पर्याप्त था।

नाम या महाद्वीप द्वारा अपने आवेदन मैं रंग contries के लिए, इसलिए इन पंक्तियों:

name = shapedict["name_long"] 
    continent = shapedict["continent"] 

डेटा का उपयोग किया है कि मैं इस वेबसाइट से मिले: http://www.naturalearthdata.com/

+2

आपका अल्बानिया डूब गया है। ऐसा नहीं है कि कई लोग नोटिस करेंगे: डी – theta

+0

हां, वास्तव में आर्मेनिया के साथ भी ऐसा ही होता है। बाद में इन दोनों देशों को स्पष्ट रूप से भरकर मुझे एक काम करना पड़ा।Naturalearthdata से लोगों के साथ पूछताछ निर्णायक नहीं थी और मैंने इसे मेरे लिए तय करने के बाद इसका पालन नहीं किया –

+0

@red_tiger मुझे अर्जेंटीना और अंगोला के साथ एक ही समस्या है। क्या आप अपना समाधान "अल्बेनियन समस्या" में पोस्ट कर सकते हैं? NaturalEarth में लोगों ने क्या कहा? धन्यवाद। –