2010年12月6日月曜日

Install GeoDjango on CentOS 64bit

==Instlal Pacage

>vi /etc/yum.repos.d/CentOS-Base.repo
[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag

>wget http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt
>rpm --import RPM-GPG-KEY.dag.txt

>yum -y install python-devel gcc gcc-c++ swig ruby yum-utils libxml2 libxml2-devel
>yum -y install postgresql84 postgresql84-devel postgresql84-server postgresql84-server-python
>yum -y install gettext python-setuptools python-devel python-tz python-curl mod_python openssl-devel python-ctypes
>yum -y install gcc gcc-c++ fonts-japanese expect sudo subversion rpm-build rpm-devel
>yum -y install httpd httpd-devel mod_ssl apr-devel zlib zlib-devel python-setuptools python-psycopg2

>yum -y install ntp libevent postfix

=GEOS

>wget wget http://download.osgeo.org/geos/geos-3.2.2.tar.bz2
>tar -xjvf geos-3.2.2.tar.bz2
>cd geos-3.2.2
>./configure
>make
>make install
>cd ..

=Proj4

>wget http://download.osgeo.org/proj/proj-4.7.0.tar.gz
>wget http://download.osgeo.org/proj/proj-datumgrid-1.5.zip

>tar xzf proj-4.7.0.tar.gz
>cd proj-4.7.0/nad
>unzip ../../proj-datumgrid-1.5.zip
>cd ..

>./configure
>make
>make install
>cd ..

=Postgis

>wget http://postgis.refractions.net/download/postgis-1.5.1.tar.gz
>tar xzf postgis-1.5.1.tar.gz
>cd postgis-1.5.1

>./configure
>make
>make install
>cd ..

=GDAL

>wget http://download.osgeo.org/gdal/gdal-1.7.2.tar.gz
>tar xzf gdal-1.7.2.tar.gz
>cd gdal-1.7.2

>./configure
>make
>make install
>cd ..

= SQLite

>wget http://www.sqlite.org/sqlite-amalgamation-3.6.22.tar.gz
>tar xzf sqlite-amalgamation-3.6.22.tar.gz
>cd sqlite-3.6.22
CFLAGS="-DSQLITE_ENABLE_RTREE=1" ./configure
>make
>make install
>cd ..

= SPATIALITE

>wget http://www.gaia-gis.it/spatialite/libspatialite-amalgamation-2.3.1.tar.gz
>wget http://www.gaia-gis.it/spatialite/spatialite-tools-2.3.1.tar.gz
>tar xzf libspatialite-amalgamation-2.3.1.tar.gz
>tar xzf spatialite-tools-2.3.1.tar.gz
>cd libspatialite-amalgamation-2.3.1
>./configure
>make
>make install
>cd ..

>cd spatialite-tools-2.3.1
>./configure
>make
>make install
>cd ..

= PYSQLITE2

>wget http://pysqlite.googlecode.com/files/pysqlite-2.6.0.tar.gz
>tar xzf pysqlite-2.6.0.tar.gz
>cd pysqlite-2.6.0

>vi setup.cfg
[build_ext]
#define=
include_dirs=/usr/local/include
library_dirs=/usr/local/lib
libraries=sqlite3
#define=SQLITE_OMIT_LOAD_EXTENSION

>python setup.py install


== Postgres

>service postgresql initdb
>/etc/init.d/postgresql start

>su - postgres
>wget http://geodjango.org/docs/create_template_postgis-1.5.sh

>ldd -d /usr/lib64/pgsql/postgis-1.5.so
>ln -s /usr/local/lib/libgeos_c.so.1.6.2 /usr/lib64/libgeos_c.so.1
>ln -s /usr/local/lib/libproj.so.0 /usr/lib64/libproj.so.0
>ln -s /usr/local/lib/libgdal.so.1 /usr/lib64/libgdal.so.1

>sh create_template_postgis-1.5.s

>createdb -T template_postgis geodjango
>createuser --createdb geo

= Mod WSGI

>wget http://modwsgi.googlecode.com/files/mod_wsgi-3.2.tar.gz
>tar zxvf mod_wsgi-3.2.tar.gz
>cd mod_wsgi-3.2
>make
>make install
>cd ..

== Httpd.conf

>vi httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so


ServerAdmin jun@127.0.0.1
ServerName 192.168.1.1

Alias /media "/home/html/geodjango/media"
WSGIScriptAlias / /home/html/geo.wsgi

DocumentRoot /home/html/geodjango

CustomLog /home/html/logs/access_log common
ErrorLog /home/html/logs/error_log


SetHandler None



SetHandler None


ExpiresActive On

ExpiresDefault "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"




== WSGI

>vi /home/html/geo.wsgi
#WSGI

import sys
import os
sys.path.append('/home/html')

os.environ['DJANGO_SETTINGS_MODULE'] = 'geodjango.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()


= PG_HBA

>vi /var/lib/pgsql/data/pg_hba.conf

local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust


>python manage.py sqlall world
>python manage.py syncdb
>python manage.py shell


=Django

>django-admin.py startproject geodjango
>cd geodjango
>python manage.py startapp world
>vim settings.py

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geodjango',
'USER': 'geo',
}
}

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.gis',
'world'
)


>mkdir world/data
>cd world/data
>wget http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
>unzip TM_WORLD_BORDERS-0.3.zip
>cd ../..
>ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp
>ogrinfo -so world/data/TM_WORLD_BORDERS-0.3.shp TM_WORLD_BORDERS-0.3



= Model of Django

>vi /home/html/geodjango/world/model.py
from django.contrib.gis.db import models

class WorldBorders(models.Model):
# Regular Django fields corresponding to the attributes in the
# world borders shapefile.
name = models.CharField(max_length=50)
area = models.IntegerField()
pop2005 = models.IntegerField('Population 2005')
fips = models.CharField('FIPS Code', max_length=2)
iso2 = models.CharField('2 Digit ISO', max_length=2)
iso3 = models.CharField('3 Digit ISO', max_length=3)
un = models.IntegerField('United Nations Code')
region = models.IntegerField('Region Code')
subregion = models.IntegerField('Sub-Region Code')
lon = models.FloatField()
lat = models.FloatField()

# GeoDjango-specific: a geometry field (MultiPolygonField), and
# overriding the default manager with a GeoManager instance.
mpoly = models.MultiPolygonField()
objects = models.GeoManager()

# So the model is pluralized correctly in the admin.
class Meta:
verbose_name_plural = "World Borders"

# Returns the string representation of the model.
def __unicode__(self):
return self.name



= Python Shell

import os
from geodjango import world
world_shp = os.path.abspath(os.path.join(os.path.dirname(world.__file__), 'data/TM_WORLD_BORDERS-0.3.shp'))

from django.contrib.gis.gdal import *
ds = DataSource(world_shp)
print ds
print len(ds)
lyr = ds[0]
print lyr
print len(lyr)
print srs
print lyr.fields
[fld.__name__ for fld in lyr.field_types]
for feat in lyr:
print feat.get('NAME'), feat.geom.num_points
lyr[0:2]
feat = lyr[234]
print feat.get('NAME')

geom = feat.geom
print geom.wkt

= load.py

>vi /home/html/geodjango/world/load.py
import os
from django.contrib.gis.utils import LayerMapping
from models import WorldBorders

world_mapping = {
'fips' : 'FIPS',
'iso2' : 'ISO2',
'iso3' : 'ISO3',
'un' : 'UN',
'name' : 'NAME',
'area' : 'AREA',
'pop2005' : 'POP2005',
'region' : 'REGION',
'subregion' : 'SUBREGION',
'lon' : 'LON',
'lat' : 'LAT',
'mpoly' : 'MULTIPOLYGON',
}

world_shp = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/TM_WORLD_BORDERS-0.3.shp'))

def run(verbose=True):
lm = LayerMapping(WorldBorders, world_shp, world_mapping,
transform=False, encoding='iso-8859-1')

lm.save(strict=True, verbose=verbose)

from world import load
load.run()


= Insert Geo information

>python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorders --srid=4326 --mapping --multi


= admin.poy

>vi /home/html/geodjango/world/admin.py
from django.contrib.gis import admin
from models import WorldBorders

admin.site.register(WorldBorders, admin.GeoModelAdmin)

= url.py

>vi /home/html/geodjango/url.py

from django.conf.urls.defaults import *
from django.contrib.gis import admin

admin.autodiscover()

urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)

= access admin page

http://localhost/admin

ラベル:


コメント:

コメントを投稿

登録 コメントの投稿 [Atom]





<< ホーム

This page is powered by Blogger. Isn't yours?

登録 投稿 [Atom]