Changeset 808

Show
Ignore:
Timestamp:
05/17/07 10:40:18 (2 years ago)
Author:
sgillies
Message:

IGeoItemSimple adapters for ATCT and catalog brains

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Geographer/trunk/configure.zcml

    r807 r808  
    1010 
    1111  <adapter factory=".geo.GeoContentSimple"/> 
     12  <adapter factory=".geo.GeoBrainSimple"/> 
    1213 
    1314  <five:implements 
     
    1617    /> 
    1718 
     19  <five:implements 
     20    class="Products.ZCatalog.CatalogBrains.AbstractCatalogBrain" 
     21    interface=".interfaces.IReadOnlyGeoreferenceableBrain" 
     22    /> 
     23 
    1824</configure> 
    1925 
  • Geographer/trunk/geo.py

    r807 r808  
    3030from zope.component import adapts, createObject 
    3131from zope.component.factory import Factory 
     32from zope.component.interfaces import ComponentLookupError 
    3233from zope.interface import implements 
    3334from zope.schema.fieldproperty import FieldProperty 
     
    3940    from zope.app.annotation.interfaces import IAnnotations 
    4041 
     42from Products.ZCatalog.CatalogBrains import AbstractCatalogBrain 
     43from Products.CMFPlone import CatalogTool as catalogtool 
     44 
    4145from Products.Geographer.interfaces import IGeometry, IGeoItemSimple 
    4246from Products.Geographer.interfaces import IGeoreferenceable 
     47from Products.Geographer.interfaces import IReadOnlyGeoreferenceableBrain 
    4348 
    4449 
     
    5358    type = FieldProperty(IGeometry['type']) 
    5459    coordinates = FieldProperty(IGeometry['coordinates']) 
    55     crs = FieldProperty(IGeometry['crs']) 
    5660 
    5761 
     
    7882    def __init__(self, context): 
    7983        """Initialize adapter.""" 
    80         self.context = context 
    81         annotations = IAnnotations(context) 
    82         self.geometry = annotations.get(ANNO_KEY) 
    83         if not self.geometry: 
    84             annotations[ANNO_KEY] = createObject(u'geographer.Geometry') 
    85             self.geometry = annotations[ANNO_KEY] 
    86             self.geometry.type = 'Point' 
    87             self.geometry.coordinates = () 
     84        try: 
     85            self.context = context 
     86            annotations = IAnnotations(context) 
     87            self.geometry = annotations.get(ANNO_KEY) 
     88            if not self.geometry: 
     89                annotations[ANNO_KEY] = createObject(u'geographer.Geometry') 
     90                self.geometry = annotations[ANNO_KEY] 
     91                self.geometry.type = 'Point' 
     92                self.geometry.coordinates = () 
     93        except: 
     94            raise ComponentLookupError(IGeoItemSimple, context) 
    8895 
    8996    def getInfo(self): 
     
    107114    info = property(getInfo,) 
    108115 
     116 
     117# Adapts catalog brains 
     118 
     119class GeoBrainSimple(object): 
     120 
     121    """Provides geo-referencing properties and the Python feature protocol. 
     122    """ 
     123    implements(IGeoItemSimple) 
     124    adapts(IReadOnlyGeoreferenceableBrain) 
     125 
     126    def __init__(self, context): 
     127        """Initialize adapter.""" 
     128        try: 
     129            self.id = context['getId'] 
     130            self.uri = context.getURL() 
     131            self.title = context['Title'] 
     132            self.description = context['Description'] 
     133            self.geometry = createObject(u'geographer.Geometry') 
     134            self.geometry.type = context['geom_type'] 
     135            self.geometry.coordinates = context['geom_coordinates'] 
     136            self.context = context 
     137        except: 
     138            raise ComponentLookupError(IGeoItemSimple, context) 
     139 
     140    def getInfo(self): 
     141        context = self.context 
     142        return { 
     143            'id': self.id, 
     144            'properties': { 
     145                'title': unicode(self.title), 
     146                'description': unicode(self.description), 
     147                'uri': self.uri, 
     148                }, 
     149            'geometry': { 
     150                'type': self.geometry.type, 
     151                'coordinates': self.geometry.coordinates, 
     152                } 
     153            } 
     154 
     155    info = property(getInfo,) 
     156 
     157 
     158# Support for indexing geo-referencing 
     159 
     160def getGeometryType(object, portal, **kw): 
     161    item = IGeoItemSimple(object, None) 
     162    if item is not None: 
     163        return item.geometry.type 
     164    return None 
     165 
     166def getCoordinates(object, portal, **kw): 
     167    item = IGeoItemSimple(object, None) 
     168    if item is not None: 
     169        return item.geometry.coordinates 
     170    return None 
     171 
     172catalogtool.registerIndexableAttribute('geom_type', getGeometryType) 
     173catalogtool.registerIndexableAttribute('geom_coordinates', getCoordinates) 
     174 
  • Geographer/trunk/interfaces.py

    r807 r808  
    3737 
    3838from zope.interface import Interface 
    39 from zope.schema import BytesLine, Choice, Dict, Id, Object, Tuple, URI 
     39from zope.schema import Choice, Dict, Id, Object, Tuple, URI 
    4040 
    4141# Marker interfaces. These are on the "for" side of a Zope3 adapter. 
     
    4545    """Marks an object that may be annotated with georeferencing properties. 
    4646    """ 
    47     
     47 
     48 
     49class IReadOnlyGeoreferenceableBrain(Interface): 
     50     
     51    """Marks a catalog brain. 
     52    """ 
     53   
    4854 
    4955class IGeoserializable(Interface): 
     
    7379    # TODO: geometries other than points 
    7480    type = Choice( 
    75         values=("Point",), 
     81        values=("Point",),  # Eventually add LineString, Polygon, etc. 
    7682        title=u"Geometry Type", 
    7783        description=u"The name of the geometry type. See " 
     
    8793        required=False, 
    8894        default=(), 
    89         ) 
    90  
    91     crs = BytesLine( 
    92         title=u"Coordinate Reference System", 
    93         description=u"The PROJ.4 format definition of a coordinate reference " 
    94         "system", 
    95         required=False, 
    96         default="epsg:4326", 
    9795        ) 
    9896 
  • Geographer/trunk/tests/adapters.txt

    r807 r808  
    33 
    44  >>> from Products.Geographer.interfaces import IGeoItemSimple 
     5  >>> folder = self.folder 
     6  >>> catalog = self.portal.portal_catalog 
     7  >>> catalog.addColumn('geom_type') 
     8  >>> catalog.addColumn('geom_coordinates') 
    59 
    610Target: an AT content object 
     
    913Add a new document and geo-reference it 
    1014 
    11   >>> self.folder.invokeFactory('Document', id='document', title=u'A Document', 
     15  >>> folder.invokeFactory('Document', id='document', title=u'A Document', 
    1216  ...   description=u'This is a document') 
    1317  'document' 
    14   >>> item = IGeoItemSimple(self.folder.document) 
     18  >>> item = IGeoItemSimple(folder.document) 
    1519  >>> item 
    1620  <Products.Geographer.geo.GeoContentSimple object at ...> 
     
    2024  >>> item.geometry.type = 'Point' 
    2125  >>> item.geometry.coordinates = ((0.0, 0.0, 0.0),) 
     26  >>> folder.document.reindexObject() 
    2227 
    2328Check info 
     
    2530  >>> item.info 
    2631  {'geometry': {'type': 'Point', 'coordinates': ((0.0, 0.0, 0.0),)}, 'id': 'document', 'properties': {'uri': 'http://nohost/plone/Members/test_user_1_/document', 'description': u'This is a document', 'title': u'A Document'}} 
     32     
     33Target: a catalog brain 
     34----------------------- 
    2735 
     36Query catalog for the test document 
     37 
     38  >>> brain = catalog.searchResults({"getId": "document"})[0] 
     39  >>> brain.geom_type 
     40  'Point' 
     41  >>> brain.geom_coordinates 
     42  ((0.0, 0.0, 0.0),) 
     43  >>> item = IGeoItemSimple(brain, None) 
     44  >>> item.geometry 
     45  <Products.Geographer.geo.Geometry object at ...> 
     46  >>> item.geometry.type 
     47  'Point' 
     48  >>> item.geometry.coordinates 
     49  ((0.0, 0.0, 0.0),) 
     50  >>> item.info 
     51  {'geometry': {'type': 'Point', 'coordinates': ((0.0, 0.0, 0.0),)}, 'id': 'document', 'properties': {'uri': 'http://nohost/plone/Members/test_user_1_/document', 'description': u'This is a document', 'title': u'A Document'}} 
     52