Archive

Posts Tagged ‘python’

Diving into Python

August 12th, 2007 rupert No comments

Yup, Im now diving into Python. So far, Im loving it. Not as hard as java, very pragmatic programming. I got converted when I saw Awaretek’s “Python Learning Foundation” which I believe deserves a lot of merit for promoting Python. One of the podcasts from www.directionmag.com states that Python is #4 among GIS professional as a must-have progamming language experience.

Moving forward, I kickstarted with a few tutorials. Fast-reading most of the tutorials from http://www.upriss.org.uk/python/PythonCourse.html and trying out the exercises. It was fabolous. Simple yet powerful. Then I saw Alex Martelli’s tutorial from http://www.aleax.it/Python/py25.pdf. Those who have programming experience like Java/C, would benefit much from Martell’s PDF tutorial.

Next stop.. using databases with python.. Head on to the database section of AwareTek.

1. I’ve dowloaded Python’s DB Interface for Postgres ‘psycopg2‘. http://www.initd.org/tracker/psycopg/wiki/PsycopgTwo

2. Steve Holden’s dbPythonIntro.pdf. Presents a quick overview of the DB API. Important pages would be:

  • 26/65 Connection Example (includes Postgres)
  • 29/65 Connection Methods
  • 30/65 Executing Queries
  • 31/65 Retrieving Results

3. Accessing PostgreSQL with Python and Psycopg2. This three page tutorial will quickly give you a background of how to execute queries, fetch queries using column index or by names.

Categories: python Tags:

Installing TileCache on RedHat/CentOS

July 25th, 2007 rupert Comments off

1. Install Apache
./configure –prefix=/usr/local/apache2 –enable-so –enable-rewrite –with-mpm=prefork
make
make install
rm -Rf /usr/local/apache2/htdocs/*

2. Install mod_python-3.3.1

./configure --with-apxs=/usr/local/apache2/bin/apxs
make
make install

3. Install Python Imaging Library (PIL) – Imaging-1.1.6

python setup.py install

Note: To check if PIL was successfully installed:
#python selftest.py
***Test Failed*** 1 failures.
*** 1 tests of 57 failed.

47 ln -s /usr/lib/libjpeg.so.62 /usr/lib/libjpeg.so
48 ldconfig
49 python setup.py install
51 python selftest.py
54 rm -rf Imaging-1.1.6
55 tar -zxvf Imaging-1.1.6.tar.gz
56 cd Imaging-1.1.6
58 python selftest.py -> still fails
59 python setup.py install
60 python selftest.py -> ok

4. Check if mod_python was sucessfully installed.
http://www.dscpl.com.au/wiki/ModPython/Articles/GettingModPythonWorking

[root@rupert-centos pytest]# python
Python 2.4.3 (#1, Mar 14 2007, 18:51:08)
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mod_python
>>> mod_python.version
'3.3.1'
>>>

4. Edit httpd.conf

<directory>
 AddHandler python-program .py
 PythonHandler TileCache.Service
 PythonOption TileCacheConfig /usr/local/apache2/htdocs/tilecache/tilecache.cfg
 PythonDebug On
 PythonPath "sys.path + ['/usr/local/apache2/htdocs/tilecache/']"
</directory>
 
<directory>
 AddHandler mod_python .py
 PythonHandler test
 PythonDebug On
</directory>

Note: This syntax will work for all versions of mod_python. In version 3.0 and later, the name of the mod_python handler reference has actually been changed and thus it is now preferred to use “mod_python” instead of “python-program”. The old name though is still supported and will be used here to avoid confusion for those using version 2.7.

5. Test your python

from mod_python import apache
 
def handler(req):
      req.log_error('handler')
      req.content_type = 'text/plain'
      req.send_http_header()
      req.write('mptest.py\n')
      return apache.OK

Transparent Overlays in TileCache

March 31st, 2007 rupert Comments off

From the tilecache mailing list….

Hi Everyone,

Really appreciate all the replies…

1. Installed Imaging-1.1.6.tar.gz (PIL).

[root@rupert-linux ~]# python
Python 2.3.4 (#1, Mar 10 2006, 06:12:09) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import sys
&gt;&gt;&gt; print sys.path
['', '/usr/lib/python23.zip', '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2', '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload', '/usr/lib/python2.3/site-packages',
'/usr/lib/python2.3/site-packages/PIL',
'/usr/lib/python2.3/site-packages/gtk-2.0']
&gt;&gt;&gt;

2. Modified test1.cfm

     41         var options = {
     42                         controls: [new OpenLayers.Control.MouseDefaults()]
     43                       };
     44
     45         map = new OpenLayers.Map( $('map'), options);
     46
     47         var layer_base = new OpenLayers.Layer.WMS(
     48                     "Base Layer",
     49                     "#request.mapserv_tile#",
     50                     {
     51                         map: '/home/map/beijing/new/wms.map',
     52                         layers: '#request.basemap_tile#',
     53                         format: 'image/png', 'transparent': 'false'
     54                     }
     55                     );
     56
     57         var layer_road = new OpenLayers.Layer.WMS(
     58                     "Road Layer",
     59                     "#request.mapserv_tile#",
     60                     {
     61                         map: '/home/map/beijing/new/wms.map',
     62                         layers: '#request.roads_tile#',
     63                         format: 'image/png', 'transparent': 'true'
     64                     },
     65                     {
     66                         reproject: false
     67                     }
     68                     );
     69
     70         map.addLayer(layer_base);
     71         layer_base.setIsBaseLayer(true);
     72
     73         layer_road.setIsBaseLayer(false);
     74         map.addLayer(layer_road);

3. tilecache.cfg. Commented metaTile=true http://222.128.19.19/tilecache/tilecache.cfg

     47 [basemap]
     48 type=WMSLayer
     49 url=http://127.0.0.1/cgi-bin/mapserv?map=/home/map/beijing/new/wms.map
     50 layers=district,greens,major_river,minor_river
     51 #bbox=-180,-90,180,90
     52 #metaTile=true
     53 extension=png
     54
     55 [roads]
     56 type=WMSLayer
     57 url=http://127.0.0.1/cgi-bin/mapserv?map=/home/map/beijing/new/wms.map
     58 #layers=road4,road4label,road3,road3label,road2,road2label,road1,road1label,road11,road11label
     59 #bbox=116.1737,39.8211,116.5640,40.0799
     60 #maxresolution=1.40625
     61 #bbox=-180,-90,180,90
     62 layers=road1,road1label,road11,road11label
     63 extension=png
     64 #metaTile=true

4. Modified wms.map http://222.128.19.19/tilecache/wms.map

OUTPUTFORMAT
     NAME png
     DRIVER "GD/PNG"
     MIMETYPE "image/png"
     IMAGEMODE RGB
     EXTENSION "png"
     FORMATOPTION "INTERLACE=OFF"
END

5. Checked access_log. “transparent=true” exists…

192.168.1.150 - - [30/Mar/2007:13:46:26 +0800] "GET /tilecache/tilecache.py?MAP=%2Fhome%2Fmap%2Fbeijing%2Fnew%2Fwms.map&LAYERS=r
oads&FORMAT=image%2Fpng&TRANSPARENT=true&SERVICE=WMS&VERSION=1.1.1&REQUEST=G
etMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BB
OX=116.334229%2C39.891357%
2C116.345215%2C39.902344&WIDTH=256&HEIGHT=256 HTTP/1.1" 200 14580

Now, this is really weird. I have “transparent”: true in test1.cfm all the time. That didn’t worked.

For every test iteration i made, I always tried clearing the cache, removing the python compiled scripts, then restarting Apache just to get a clean state..


rm -Rf /usr/local/apache2/htdocs/tmp/*
rm /wwwroot/tilecache/TileCache/*.pyc
/etc/init.d/httpd restart

Then I tried appending “transparent=true” to tilecache.cfg based on Eric’s suggestion…
That worked. Now I wonder why… Nevertheless, its working now. Again many thanks to everyone…

Rupert

On Fri, Mar 30, 2007 at 01:57:43PM +0800, Rupert de Guzman Jr wrote:
> Hi Everyone,
>
> Really appreciate all the replies…
> Then I tried appending “transparent=true” to tilecache.cfg based on
> Eric’s suggestion…
> That worked. Now I wonder why… Nevertheless, its working now. Again
> many thanks to everyone…

TileCache pays almost no attention to the URL: Only the BBOX and the layername matter. Anything else is simply ignored: so your transparency being set in OpenLayers actually has 0 affect.

The reason for this is that TileCache can only store one copy of an image. If the URL parameters modified the content, you could get an inconsistent cache.

This is ‘by design’, insofar as there is no obvious solution (other than to complain more loudly when TC Gets parameters it isn’t expecting, which is an outstanding FIXME in the code). The lack of error message is not by design, and I’m sorry you got bit by the poorly documented behavior.

Regards,

Christopher Schmidt
MetaCarta

Setting python_path on Unix

March 29th, 2007 rupert Comments off

At the time of this writing.. download mod_python-3.3.1.tgz

./configure –with-apxs=/usr/local/apache2/bin/apxs
make
make install

You need to pass the PYTHONPATH to apache..

AddHandler python-program .py
PythonHandler TileCache.Service
PythonOption TileCacheConfig /usr/local/apache2/htdocs/tilecache/tilecache.cfg
PythonDebug On
PythonPath “sys.path + ['/usr/local/apache2/htdocs/tilecache/']”

In your bash shells, you could also check the PYTHONPATH…

[root@rupert-linux views]# python
Python 2.3.4 (#1, Mar 10 2006, 06:12:09)
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import sys
&gt;&gt;&gt; print sys.path
['', '/usr/lib/python23.zip', '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2', '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload', '/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']
&gt;&gt;&gt;
Categories: mapserver, tilecache Tags: ,

Installing MS4W, Python2.4, ColdFusion6.1

January 17th, 2007 rupert Comments off

For Windows:

A. Installing MS4W
1. Download ms4w2.2 from maptools.org

2. Extract ms4w2.2 into your root drive (E:\)

3. Run E:\ms4w\apache-install.bat. This would install apache2.2 as a service.

4. Edit E:\ms4w\Apache\conf\httpd.conf to reflect your webroot in “DocumentRoot” (E:\wwwroot\)

149 DocumentRoot "/wwwroot"
...
....
177 <directory>
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
 
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None
 
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
205 </directory>
....
....
212 DirectoryIndex index.html index.html.var index.php index.phtml index.php3 index.cfm
....
....

5. Test apache, navigate to “http://127.0.0.1/”.

B. Configuring Apache2.2 with ColdFusion6.1
6. Since Apache2.2 was released after CF6.1 and CF7.0 then, it would only bind with Apache2.0.x. We need a new wsconfig.jar that would bind with Apache2.2.x. Read more about the technote:
http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=8001e97

Alternatively, you could download wsconfig.jar from http://www.adobe.com/support/coldfusion/ts/documents/8001e97/wsconfig.zip

7. Backup your existing wsconfig.jar to wsconfig.jar.bak. Replace the wsconfig.jar.

8. Restart ColdFusion.

9. Remove existing connectors from D:\CFusionMX\bin\connectors\Remove_ALL_connectors.bat

10. Execute D:\CFusionMX\bin\connectors\Apache_connector.bat

@echo off
echo WARNING!  This will install the ColdFusion MX Apache Connector.
echo Press Control+C to abort.
pause
SETLOCAL
PATH=..\..\runtime\jre\bin;%PATH%
java -jar ..\..\runtime\lib\wsconfig.jar  -ws apache -bin "e:\\ms4w\Apache\bin\httpd.exe" -dir "e:\\ms4w\apache\conf" -map .cfm,.cfc,.cfml -coldfusion -v
ENDLOCAL

11. Restart Apache and ColdFusion. Browse to http://127.0.0.1/cfide/administrator/index.cfm

C. Installing Python2.4
1. Read Reference document from maptools.org install site

2. Download Python2.4 from http://www.python.org/ftp/python/2.4/python-2.4.msi. Download mod_python from mod_python-3.3.0b.win32-py2.4-Apache2.2.exe

3. Install Python2.4 in your root drive. (D:\)

4. Install mod_python-3.3.0b.win32-py2.4-Apache2.2.exe

5. Edit your Windows Environment Variables (System Variable: PATH) to include “D:\Python23″

6. Edit E:\ms4w\Apache\conf\httpd.conf

#For Python
LoadModule python_module modules/mod_python.so
 
<directory>
	AddHandler python-program .py
	PythonHandler test
	PythonDebug On
</directory>
 
ScriptInterpreterSource Registry
SetEnv PYTHONUNBUFFERED 1
PassEnv PYTHONPATH

7. Restart Apache and test from http://127.0.0.1/pytest/test.py. If you see a “Hello World” web page from python then you are good to go. Sample python test page “test.py”:
E:\wwwroot\pytest\test.py

from mod_python import apache
 
def handler(req):
	req.content_type = 'text/plain'
	req.write("Hello World!")
	return apache.OK
Categories: mapserver, ms4w Tags: , ,