Home > World Of ICT > How to install Python Support Basemap, Matplotlib, Numpy, Scipy, On FreeBSD Pastinya brad..

How to install Python Support Basemap, Matplotlib, Numpy, Scipy, On FreeBSD Pastinya brad..


Posting perdana saya di bulan Juli 2012 ini masih mengangkat tema mengenai Uler PythonūüėÄ , kali ini saya mencoba untuk mendokumentasikan perjalanan panjang nan berliku liku bagaimana menjalankan Python yang support library Basemap, Matplotlib, Numpy, Scipy pada environment FreeBSD dengan tujuan akhir agar dapat oprak oprek scripting GIS. Lets do it.

  • Saya menggunakan FreeBSD 9.0 Release.
  • Install Python 2.7 via port
BGP-UNILA# cd /usr/ports/lang/python27/
BGP-UNILA# make && make install
 ===>  Vulnerability check disabled, database not found
 ===>  License check disabled, port has not defined LICENSE
 ===>  Found saved configuration for python27-2.7.2_3
 ===>  Extracting for python27-2.7.2_3
 => SHA256 Checksum OK for python/Python-2.7.2.tgz.
 ===>  Patching for python27-2.7.2_3
 ===>  Configuring for python27-2.7.2_3
 checking for --enable-universalsdk... no
 checking for --with-universal-archs... 32-bit
 checking MACHDEP... freebsd9
 checking EXTRAPLATDIR...
 checking machine type as reported by uname -m... amd64
  • Install Matplotlib.
BGP-UNILA# cd /usr/ports/math/py-matplotlib
BGP-UNILA# make && make install
 ===>  Vulnerability check disabled, database not found
 ===>  License check disabled, port has not defined LICENSE
 ===>  Found saved configuration for py27-matplotlib-1.0.1_3
 ===>  Extracting for py27-matplotlib-1.0.1_3
 => SHA256 Checksum OK for matplotlib-1.0.1.tar.gz.
 /bin/chmod -R ga+r /usr/ports/math/py-matplotlib/work
 ===>  Patching for py27-matplotlib-1.0.1_3
 ===>  Applying FreeBSD patches for py27-matplotlib-1.0.1_3
 ===>   py27-matplotlib-1.0.1_3  
  • Install numpy.
BGP-UNILA# cd /usr/ports/math/py-numpy
BGP-UNILA# make && make install
 ===>  Vulnerability check disabled, database not found
 ===>  License check disabled, port has not defined LICENSE
 ===>  Found saved configuration for py27-scipy-0.10.0
 ===>  Extracting for py27-numpy-1.6.1,1
 => SHA256 Checksum OK for numpy-1.6.1.tar.gz.
 => SHA256 Checksum OK for numpy-ref.pdf.
 => SHA256 Checksum OK for numpy-user.pdf.
 ===>  Patching for py27-numpy-1.6.1,1
 ===>  Applying FreeBSD patches for py27-numpy-1.6.1,1
 ===>   py27-numpy-1.6.1,1 depends on file: /usr/local/bin/python2.7 - found
 ===>   py27-numpy-1.6.1,1 depends on executable: gcc46 - found
 ===>   py27-numpy-1.6.1,1 depends on file: /usr/local/bin/as - found
 ===>   py27-numpy-1.6.1,1 depends on shared library: lapack.4 - found
 ===>   py27-numpy-1.6.1,1 depends on shared library: blas.2 - found
 ===>  Configuring for py27-numpy-1.6.1,1
 Running from numpy source directory.svnversion: not found
 F2PY Version 2
 blas_opt_info:
 blas_mkl_info:
  • Install Scipy.
BGP-UNILA# cd /usr/ports/science/py-scipy
BGP-UNILA# make && make install
 ===>  Building for py27-scipy-0.10.0
 Running from scipy source directory.
 ^CTraceback (most recent call last):
   File "setup.py", line 196, in <module>
     setup_package()
   File "setup.py", line 187, in setup_package
     configuration=configuration )
   File "/usr/local/lib/python2.7/site-packages/numpy/distutils/core.py", line 152, in setup
     config = configuration()
   File "setup.py", line 138, in configuration
     config.add_subpackage('scipy')
     ('.py', 'U', 1))
   File "scipy/setup.py", line 2, in <module>
     def configuration(parent_package='',top_path=None):
  • Install Basemap.

Khusus untuk instalasi Basemap, pada FreeBSD  harus dilakukan instalasi secara manual, karena apabila anda menggunakan port, python tidak akan bisa membaca library Basemap, kira-kira tampilannya akan sebagai berikut

BGP-UNILA# python
 Python 2.5.7 (r252:60911, Feb 11 2009, 10:50:43)
 [GCC 4.2.1 20070719  [FreeBSD]] on freebsd7
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import basemap
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 ImportError: No module named basemap
 >>>

Silahkan download Basemap versi terbaru dari source forge http://sourceforge.net/projects/matplotlib/files/matplotlib-toolkits/ dan lakukan proses instalasi secara manual

Contoh disaya saya menggunakan Basemap versi 1.0.1 (basemap-1.0.1.tar.gz),

BGP-UNILA# pwd
 /usr/home/gigih/python
BGP-UNILA# ll basemap-1.0.1.tar.gz
 -rw-r--r--  1 root  wheel  103490425 Feb 10  2011 basemap-1.0.1.tar.gz
BGP-UNILA# tar -zxvf basemap-1.0.1.tar.gz
BGP-UNILA# cd basemap-1.0.1
BGP-UNILA# python setup.py install
 checking to see if required version of pydap installed ..
 pydap installed, checking version ...
 pydap version OK, will not be installed
 checking to see if httplib2 installed ..
 httplib2 installed
 checking to see if pyshapelib installed ..
 pyshapelib installed
 running install
 running build
 running config_cc
 unifing config_cc, config, build_clib, build_ext, build commands --compiler options
 running config_fc
 unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
 running build_src
 build_src
 building extension "mpl_toolkits.basemap._proj" sources
 building extension "mpl_toolkits.basemap._geod" sources
 building extension "_geoslib" sources
 build_src: building npy-pkg config files
 running build_py

Sampai disini anda telah berhasil melengkapi python yang support library Basemap, Matplotlib, Numpy, Scipy, sekarang tinggal kita ujicoba dengan membuat script sederhana,  buat file coba.py dengan isi program sbb, lalu jalankan dengan mengetikkan python coba.py

#!/usr/bin/env python
 from mpl_toolkits.basemap import Basemap
 import matplotlib.pyplot as plt
 import numpy as np
 # set up orthographic map projection with
 # perspective of satellite looking down at 50N, 100W.
 # use low resolution coastlines.
 map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
 # draw coastlines, country boundaries, fill continents.
 map.drawcoastlines(linewidth=0.25)
 map.drawcountries(linewidth=0.25)
 map.fillcontinents(color='coral',lake_color='aqua')
 # draw the edge of the map projection region (the projection limb)
 map.drawmapboundary(fill_color='aqua')
 # draw lat/lon grid lines every 30 degrees.
 map.drawmeridians(np.arange(0,360,30))
 map.drawparallels(np.arange(-90,90,30))
 # make up some data on a regular lat/lon grid.
 nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
 lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
 lons = (delta*np.indices((nlats,nlons))[1,:,:])
 wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
 mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)
 # compute native map projection coordinates of lat/lon grid.
 x, y = map(lons*180./np.pi, lats*180./np.pi)
 # contour data over the map.
 cs = map.contour(x,y,wave+mean,15,linewidths=1.5)
 plt.title('contour lines over filled continent background')
 plt.savefig('tes.png', format='png')

Apabila anda menjalankan script ini tanpa Xwindow terinstall di FreeBSD, maka akan anda temui warning error sebagai berikut

BGP-UNILA# python coba.py
Traceback (most recent call last):
  File "coba.py", line 10, in <module> map.drawcoastlines(linewidth=0.25)
  File "/usr/local/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.py", line 1495, 
  in drawcoastlines   ax = ax or self._check_ax()
  File "/usr/local/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.py", line 3689, 
  in _check_ax    ax = plt.gca()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 593, in gca
    ax =  gcf().gca(**kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 292, in gcf
    return figure()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 270, in figure
    **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 80, 
  in new_figure_manager  window = Tk.Tk()
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1685, in __init__
    self.tk = _tkinter.create
    (screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
BGP-UNILA#

Solusinya adalah anda perlu install X-Window atau dengan mengikuti trik berikut

import matplotlib
matplotlib.use('Agg') # 
import pylab
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

Hasil akhir dari script ini adalah sebagai berikut;

Categories: World Of ICT
  1. July 12, 2013 at 5:06 am

    Hello there, You have done a great job. I will certainly
    digg it and personally suggest to my friends. I’m sure they’ll be benefited from this website.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: