# HG changeset patch
# User Pavel Shramov <psha@void.psha.org.ru>
# Date 1337519404 -14400
# Node ID 486e783530a89a63a91d19c2c0961edbeb899b01
# Parent  8bccd71ebb62a86ad9fbc1d3557ec90060a28d0e
Parse datasource params from config file

If datasource section not found behave like old komap
Otherwise load parameters from that section

diff -r 8bccd71ebb62 -r 486e783530a8 src/komap.py
--- a/src/komap.py	Fri May 18 23:49:57 2012 +0400
+++ b/src/komap.py	Sun May 20 17:10:04 2012 +0400
@@ -76,8 +76,6 @@
 libkomapnik.map_proj = config.get("mapnik", "map_proj")
 libkomapnik.db_proj = config.get("mapnik", "db_proj")
 libkomapnik.table_prefix = config.get("mapnik", "table_prefix")
-libkomapnik.db_user = config.get("mapnik", "db_user")
-libkomapnik.db_name = config.get("mapnik", "db_name")
 libkomapnik.db_srid = config.get("mapnik", "db_srid")
 libkomapnik.icons_path = config.get("mapnik", "icons_path")
 libkomapnik.world_bnd_path = config.get("mapnik", "world_bnd_path")
@@ -86,6 +84,21 @@
 libkomapnik.cleantopo_hs_path = config.get("mapnik", "cleantopo_hs_path")
 libkomapnik.srtm_hs_path = config.get("mapnik", "srtm_hs_path")
 
+if not config.has_section('datasource'):
+  libkomapnik.db_user = config.get("mapnik", "db_user")
+  libkomapnik.db_name = config.get("mapnik", "db_name")
+  libkomapnik.db_params = \
+      { 'type':'postgis'
+      , 'st_prefix':'true'
+      , 'user':config.get("mapnik", "db_user")
+      , 'dbname':config.get("mapnik", "db_name")
+      , 'srid':config.get("mapnik", "db_srid")
+      , 'estimate_extent':'false'
+      , 'geometry_field':'way'
+      }
+else:
+  for o in config.options('datasource'):
+    libkomapnik.db_params[o] = config.get('datasource', o)
 
 from libkomapnik import *
 
diff -r 8bccd71ebb62 -r 486e783530a8 src/libkomapnik.py
--- a/src/libkomapnik.py	Fri May 18 23:49:57 2012 +0400
+++ b/src/libkomapnik.py	Sun May 20 17:10:04 2012 +0400
@@ -35,6 +35,8 @@
 cleantopo_hs_path = ""
 srtm_hs_path = ""
 
+db_params = {}
+
 
 substyles = []
 
@@ -312,16 +314,29 @@
     <CssParameter name="stroke-dasharray">0,18,1,155</CssParameter>
   </LineSymbolizer>"""
 
+def xml_dict_to_param(params):
+  r = []
+  shift = ' ' * 8
+  for k,v in params.items():
+    r.append('<Parameter name="%s">%s</Parameter>' % (k, v))
+  return shift + ("\n" + shift).join(r)
+
 def xml_layer(type="postgis", geom="point", interesting_tags = "*", sql = "true", zoom=0 ):
   layer_id = get_id(1)
   global substyles
   subs = "\n".join(["<StyleName>s%s</StyleName>"%i for i in substyles])
   substyles = []
-  intersection_SQL = ""
+  
+  params = dict(db_params)
+  params.update({ 'geometry_table':table_prefix + geom
+                , 'extent': "-20037508.342789244, -20037508.342780735, 20037508.342789244, 20037508.342780709"
+                })
   if zoom < 5:
-    intersection_SQL = '<Parameter name="intersect_max_scale">1</Parameter>'
+    params['intersect_max_scale'] = 1
   elif zoom > 16:
-    intersection_SQL = '<Parameter name="intersect_min_scale">500000000000</Parameter>'
+    params['intersect_max_scale'] = 500000000000
+  intersection_SQL = xml_dict_to_param(params)
+
   if type == "postgis":
     interesting_tags = list(interesting_tags)
     if '"' not in "".join(interesting_tags) and "->" not in "".join(interesting_tags):
@@ -342,17 +357,8 @@
         ) as text
         </Parameter>
         %s
-        <Parameter name="type">postgis</Parameter>
-        <Parameter name="st_prefix">true</Parameter>
-        <Parameter name="user">%s</Parameter>
-        <Parameter name="dbname">%s</Parameter>
-        <Parameter name="srid">%s</Parameter>
-        <Parameter name="geometry_field">way</Parameter>
-        <Parameter name="geometry_table">%s%s</Parameter>
-        <Parameter name="estimate_extent">false</Parameter>
-        <Parameter name="extent">-20037508.342789244, -20037508.342780735, 20037508.342789244, 20037508.342780709</Parameter>
       </Datasource>
-    </Layer>"""%(layer_id, db_proj, subs, interesting_tags, table_prefix, geom, sql, intersection_SQL, db_user, db_name, db_srid,  table_prefix, geom)
+    </Layer>"""%(layer_id, db_proj, subs, interesting_tags, table_prefix, geom, sql, intersection_SQL)
   elif type == "postgis-process":
     return """
     <Layer name="l%s" status="on" srs="%s">
@@ -363,17 +369,8 @@
         ) as text
         </Parameter>
         %s
-        <Parameter name="type">postgis</Parameter>
-        <Parameter name="st_prefix">true</Parameter>
-        <Parameter name="user">%s</Parameter>
-        <Parameter name="dbname">%s</Parameter>
-        <Parameter name="srid">%s</Parameter>
-        <Parameter name="geometry_field">way</Parameter>
-        <Parameter name="geometry_table">%s%s</Parameter>
-        <Parameter name="estimate_extent">false</Parameter>
-        <Parameter name="extent">-20037508.342789244, -20037508.342780735, 20037508.342789244, 20037508.342780709</Parameter>
       </Datasource>
-    </Layer>"""%(layer_id, db_proj, subs, sql, intersection_SQL, db_user, db_name, db_srid,  table_prefix, geom)
+    </Layer>"""%(layer_id, db_proj, subs, sql, intersection_SQL)
   elif type == "coast":
     if zoom < 9:
       return """
@@ -398,4 +395,4 @@
   substyles = []
 def xml_nosubstyle():
   global substyles
-  substyles = substyles[:-1]
\ No newline at end of file
+  substyles = substyles[:-1]
