~erobererunc/sahana-eden/tropo_xforms

« back to all changes in this revision

Viewing changes to controllers/gis.py

  • Committer: Fran Boon
  • Date: 2010-11-28 23:18:54 UTC
  • Revision ID: fran@aidiq.com-20101128231854-87h940t7m4i2tkxc
GIS: GeoExplorer integration progressed: can now save & load WMCs; some configurable map options & CSS cleanups

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
    else:
71
71
        wms_browser = None
72
72
 
 
73
    # 'normal', 'mgrs' or 'off'
 
74
    mouse_position = deployment_settings.get_gis_mouse_position()
 
75
 
 
76
    # http://eden.sahanafoundation.org/wiki/BluePrintGISPrinting
 
77
    print_service = deployment_settings.get_gis_print_service()
 
78
    if print_service:
 
79
        print_tool = {url: print_service}
 
80
    else:
 
81
        print_tool = {}
 
82
    
73
83
    # Custom Feature Layers
74
84
    feature_queries = []
75
85
    feature_layers = db(db.gis_layer_feature.enabled == True).select()
86
96
                       search=search,
87
97
                       catalogue_overlays=catalogue_overlays,
88
98
                       feature_queries=feature_queries,
89
 
                       #mouse_position = "mgrs"
 
99
                       mouse_position = mouse_position,
 
100
                       print_tool = print_tool
90
101
                      )
91
102
 
92
103
    return map
349
360
        item = s3xrc.xml.json_message(False, 404, "Record not found!")
350
361
        raise HTTP(404, body=item)
351
362
 
352
 
    import gluon.contrib.simplejson as json
353
 
 
354
363
    # Find all tables which link to the Locations table
355
364
    # @ToDo Replace with db.gis_location._referenced_by
356
365
    tables = shn_table_links("gis_location")
1622
1631
 
1623
1632
    """
1624
1633
        Custom View for GeoExplorer: http://projects.opengeo.org/geoext/wiki/GeoExplorer
1625
 
        This version doesn't yet work properly
1626
1634
    """
1627
1635
 
1628
1636
    google_key = db(db.gis_apikey.name == "google").select(db.gis_apikey.apikey, limitby=(0, 1)).first().apikey
1629
1637
    
1630
 
    return dict(google_key=google_key)
 
1638
    # http://eden.sahanafoundation.org/wiki/BluePrintGISPrinting
 
1639
    print_service = deployment_settings.get_gis_print_service()
 
1640
 
 
1641
    geoserver_url = deployment_settings.get_gis_geoserver_url()
 
1642
 
 
1643
    return dict(google_key=google_key, print_service=print_service, geoserver_url=geoserver_url)
 
1644
 
 
1645
def about():
 
1646
    """  Custom View for GeoExplorer """
 
1647
    return dict()
 
1648
 
 
1649
def maps():
 
1650
 
 
1651
    """
 
1652
        Map Save/Publish Handler for GeoExplorer
 
1653
    """
 
1654
 
 
1655
    if request.env.request_method == "GET":
 
1656
        # This is a request to read the config of a saved map
 
1657
 
 
1658
        # Which map are we updating?
 
1659
        id = request.args(0)
 
1660
        if not id:
 
1661
            raise HTTP(501)
 
1662
 
 
1663
        # Read the WMC record
 
1664
        record = db(db.gis_wmc.id == id).select(limitby=(0, 1)).first()
 
1665
        # & linked records
 
1666
        #projection = db(db.gis_projection.id == record.projection).select(limitby=(0, 1)).first()
 
1667
 
 
1668
        # Put details into the correct structure
 
1669
        output = dict()
 
1670
        output["map"] = dict()
 
1671
        map = output["map"]
 
1672
        map["center"] = [record.lat, record.lon]
 
1673
        map["zoom"] = record.zoom
 
1674
        # @ToDo: Read Projection (we generally use 900913 & no way to edit this yet)
 
1675
        map["projection"] = "EPSG:900913"
 
1676
        map["units"] = "m"
 
1677
        map["maxResolution"] = 156543.0339
 
1678
        map["maxExtent"] = [ -20037508.34, -20037508.34, 20037508.34, 20037508.34 ]
 
1679
        # @ToDo: Read Layers
 
1680
        map["layers"] = []
 
1681
        #map["layers"].append(dict(source="google", title="Google Terrain", name="TERRAIN", group="background"))
 
1682
        #map["layers"].append(dict(source="ol", group="background", fixed=True, type="OpenLayers.Layer", args=[ "None", {"visibility":False} ]))
 
1683
        for _layer in record.layer_id:
 
1684
            layer = db(db.gis_wmc_layer.id == _layer).select(limitby=(0, 1)).first()
 
1685
            if layer.type_ == "OpenLayers.Layer":
 
1686
                # Add args
 
1687
                map["layers"].append(dict(source=layer.source, title=layer.title, name=layer.name, group=layer.group_, type=layer.type_, format=layer.format, visibility=layer.visibility, transparent=layer.transparent, opacity=layer.opacity, fixed=layer.fixed, args=[ "None", {"visibility":False} ]))
 
1688
            else:
 
1689
                map["layers"].append(dict(source=layer.source, title=layer.title, name=layer.name, group=layer.group_, type=layer.type_, format=layer.format, visibility=layer.visibility, transparent=layer.transparent, opacity=layer.opacity, fixed=layer.fixed))
 
1690
        
 
1691
        # @ToDo: Read Metadata (no way of editing this yet)
 
1692
 
 
1693
        # Encode as JSON
 
1694
        output = json.dumps(output)
 
1695
 
 
1696
        # Output to browser
 
1697
        response.headers["Content-Type"] = "text/json"
 
1698
        return output
 
1699
 
 
1700
    elif request.env.request_method == "POST":
 
1701
        # This is a request to save/publish a new map
 
1702
 
 
1703
        # Get the data from the POST
 
1704
        source = request.body.read()
 
1705
        if isinstance(source, basestring):
 
1706
            from StringIO import StringIO
 
1707
            source = StringIO(source)
 
1708
 
 
1709
        # Decode JSON
 
1710
        source = json.load(source)
 
1711
        # @ToDo: Projection (we generally use 900913 & no way to edit this yet)
 
1712
        lat = source["map"]["center"][0]
 
1713
        lon = source["map"]["center"][1]
 
1714
        zoom = source["map"]["zoom"]
 
1715
        # Layers
 
1716
        layers = []
 
1717
        for layer in source["map"]["layers"]:
 
1718
            try:
 
1719
                opacity = layer["opacity"]
 
1720
            except:
 
1721
                opacity = None
 
1722
            try:
 
1723
                name = layer["name"]
 
1724
            except:
 
1725
                name = None
 
1726
            _layer = db((db.gis_wmc_layer.source == layer["source"]) &
 
1727
                        (db.gis_wmc_layer.name == name) &
 
1728
                        (db.gis_wmc_layer.visibility == layer["visibility"]) &
 
1729
                        (db.gis_wmc_layer.opacity == opacity)
 
1730
                       ).select(db.gis_wmc_layer.id,
 
1731
                                limitby=(0, 1)).first()
 
1732
            if _layer:
 
1733
                # This is an existing layer
 
1734
                layers.append(_layer.id)
 
1735
            else:
 
1736
                # This is a new layer
 
1737
                try:
 
1738
                    type_ = layer["type"]
 
1739
                except:
 
1740
                    type_ = None
 
1741
                try:
 
1742
                    group_ = layer["group"]
 
1743
                except:
 
1744
                    group_ = None
 
1745
                try:
 
1746
                    fixed = layer["fixed"]
 
1747
                except:
 
1748
                    fixed = None
 
1749
                try:
 
1750
                    format = layer["format"]
 
1751
                except:
 
1752
                    format = None
 
1753
                try:
 
1754
                    transparent = layer["transparent"]
 
1755
                except:
 
1756
                    transparent = None
 
1757
                # Add a new record to the gis_wmc_layer table
 
1758
                _layer = db.gis_wmc_layer.insert(source=layer["source"], name=name, visibility=layer["visibility"], opacity=opacity, type_=type_, title=layer["title"], group_=group_, fixed=fixed, transparent=transparent, format=format)
 
1759
                layers.append(_layer)
 
1760
            
 
1761
        # @ToDo: Metadata (no way of editing this yet)
 
1762
 
 
1763
        # Save a record in the WMC table
 
1764
        id = db.gis_wmc.insert(lat=lat, lon=lon, zoom=zoom, layer_id=layers)
 
1765
 
 
1766
        # Return the ID of the saved record for the Bookmark
 
1767
        output = json.dumps(dict(id=id))
 
1768
        return output
 
1769
 
 
1770
    elif request.env.request_method == "PUT":
 
1771
        # This is a request to save/publish an existing map
 
1772
 
 
1773
        # Which map are we updating?
 
1774
        id = request.args(0)
 
1775
        if not id:
 
1776
            raise HTTP(501)
 
1777
 
 
1778
        # Get the data from the PUT
 
1779
        source = request.body.read()
 
1780
        if isinstance(source, basestring):
 
1781
            from StringIO import StringIO
 
1782
            source = StringIO(source)
 
1783
        # Decode JSON
 
1784
        source = json.load(source)
 
1785
        # @ToDo: Projection (unlikely to change)
 
1786
        lat = source["map"]["center"][0]
 
1787
        lon = source["map"]["center"][1]
 
1788
        zoom = source["map"]["zoom"]
 
1789
        # Layers
 
1790
        layers = []
 
1791
        for layer in source["map"]["layers"]:
 
1792
            try:
 
1793
                opacity = layer["opacity"]
 
1794
            except:
 
1795
                opacity = None
 
1796
            try:
 
1797
                name = layer["name"]
 
1798
            except:
 
1799
                name = None
 
1800
            _layer = db((db.gis_wmc_layer.source == layer["source"]) &
 
1801
                        (db.gis_wmc_layer.name == name) &
 
1802
                        (db.gis_wmc_layer.visibility == layer["visibility"]) &
 
1803
                        (db.gis_wmc_layer.opacity == opacity)
 
1804
                       ).select(db.gis_wmc_layer.id,
 
1805
                                limitby=(0, 1)).first()
 
1806
            if _layer:
 
1807
                # This is an existing layer
 
1808
                layers.append(_layer.id)
 
1809
            else:
 
1810
                # This is a new layer
 
1811
                try:
 
1812
                    type_ = layer["type"]
 
1813
                except:
 
1814
                    type_ = None
 
1815
                try:
 
1816
                    group_ = layer["group"]
 
1817
                except:
 
1818
                    group_ = None
 
1819
                try:
 
1820
                    fixed = layer["fixed"]
 
1821
                except:
 
1822
                    fixed = None
 
1823
                try:
 
1824
                    format = layer["format"]
 
1825
                except:
 
1826
                    format = None
 
1827
                try:
 
1828
                    transparent = layer["transparent"]
 
1829
                except:
 
1830
                    transparent = None
 
1831
                # Add a new record to the gis_wmc_layer table
 
1832
                _layer = db.gis_wmc_layer.insert(source=layer["source"], name=name, visibility=layer["visibility"], opacity=opacity, type_=type_, title=layer["title"], group_=group_, fixed=fixed, transparent=transparent, format=format)
 
1833
                layers.append(_layer)
 
1834
        
 
1835
        # @ToDo: Metadata (no way of editing this yet)
 
1836
 
 
1837
        # Update the record in the WMC table
 
1838
        db(db.gis_wmc.id == id).update(lat=lat, lon=lon, zoom=zoom, layer_id=layers)
 
1839
 
 
1840
        # Return the ID of the saved record for the Bookmark
 
1841
        output = json.dumps(dict(id=id))
 
1842
        return output
 
1843
 
 
1844
    # Abort - we shouldn't get here
 
1845
    raise HTTP(501)
1631
1846
 
1632
1847
# -----------------------------------------------------------------------------
1633
1848
def potlatch2():