def test_marker_image(self):
     manager = IGeoFeatureStyle(self.doc, None)
     manager.set('marker_image', 'test.png')
     manager.set('use_custom_styles', True)
     geojson_view = getMultiAdapter(
         (self.doc, self.doc.REQUEST),
         name='geo-json.json'
     )
     geojson = geojson_view.__of__(self.portal)()
     self.assertTrue('"image": "http://nohost/plone/test.png"' in geojson)
    def test_geo_feature_style(self):
        geomap = GeoMap(self.doc, self.doc.REQUEST)

        geo_feature_style = geomap.geo_feature_style
        self.assertEqual(
            geo_feature_style['marker_image'],
            "http://nohost/plone/++resource++collective.js.leaflet/images/marker-icon.png",
            'Incorect default marker image url')

        registry = getUtility(IRegistry)
        registry['collective.geo.settings.interfaces.IGeoFeatureStyle.linewidth'] = float(3.0)

        manager = IGeoFeatureStyle(self.doc, None)
        manager.set('use_custom_styles', False)
        manager.set('linewidth', float(11.0))
        self.doc.reindexObject(idxs=['zgeo_geometry', 'collective_geo_styles'])

        geomap = GeoMap(self.doc, self.doc.REQUEST)
        geo_feature_style = geomap.geo_feature_style

        self.assertEqual(geo_feature_style["linewidth"], 3.0)

        manager.set('use_custom_styles', True)
        self.doc.reindexObject(idxs=['zgeo_geometry', 'collective_geo_styles'])

        geomap = GeoMap(self.doc, self.doc.REQUEST)
        geo_feature_style = geomap.geo_feature_style

        self.assertEqual(geo_feature_style["linewidth"], 11.0)
def get_feature_styles(context):
    fields = [i for i in getFields(IGeoFeatureStyle)]
    manager = IGeoFeatureStyle(context, None)
    use_custom_styles = getattr(manager, 'use_custom_styles', False)
    if not use_custom_styles:
        registry = getUtility(IRegistry)
        manager = registry.forInterface(IGeoFeatureStyle)
    styles = {'use_custom_styles': use_custom_styles}
    for name in fields:
        styles[name] = getattr(manager, name, None)

    return styles
 def geo_feature_style(self):
     fields = [i for i in getFields(IGeoFeatureStyle)]
     manager = IGeoFeatureStyle(self.context, None)
     use_custom_styles = getattr(manager, 'use_custom_styles', False)
     if not use_custom_styles:
         manager = utils.geo_styles(self.context)
     styles = {'use_custom_styles': use_custom_styles}
     for name in fields:
         styles[name] = getattr(manager, name, None)
     styles['marker_image'] = get_marker_image(self.context,
                                               styles['marker_image'])
     return styles
    def __init__(self, context, request):
        self.context = context
        self.request = request

        try:
            self.geom = IGeoreferenced(self.context)
        except:
            self.geom = NullGeometry()

        try:
            self.styles = IGeoFeatureStyle(context).geostyles
        except:
            self.styles = None
    def test_custom_styles(self):

        registry = getUtility(IRegistry)
        registry['collective.geo.settings.interfaces.IGeoFeatureStyle.linewidth'] = float(3.0)

        manager = IGeoFeatureStyle(self.doc, None)
        manager.set('use_custom_styles', False)
        manager.set('linewidth', float(11.0))
        self.doc.reindexObject(idxs=['zgeo_geometry', 'collective_geo_styles'])
        geojson_view = getMultiAdapter(
            (self.doc, self.doc.REQUEST),
            name='geo-json.json'
        )
        geojson = geojson_view.__of__(self.portal)()
        self.assertTrue('"width": 3.0' in geojson)

        manager.set('use_custom_styles', True)
        self.doc.reindexObject(idxs=['zgeo_geometry', 'collective_geo_styles'])
        geojson_view = getMultiAdapter(
            (self.doc, self.doc.REQUEST),
            name='geo-json.json'
        )
        geojson = geojson_view.__of__(self.portal)()
        self.assertTrue('"width": 11.0' in geojson)
class Placemark(Feature):
    implements(IPlacemark)
    __name__ = 'kml-placemark'

    def __init__(self, context, request):
        self.context = context
        self.request = request

        try:
            self.geom = IGeoreferenced(self.context)
        except:
            self.geom = NullGeometry()

        try:
            self.styles = IGeoFeatureStyle(context).geostyles
        except:
            self.styles = None

    def __call__(self):
        return self.template().encode('utf-8')

    @property
    def hasPoint(self):
        return int(self.geom.type == 'Point')

    @property
    def hasLineString(self):
        return int(self.geom.type == 'LineString')

    @property
    def hasPolygon(self):
        return int(self.geom.type == 'Polygon')

    @property
    def hasMultiPoint(self):
        return int(self.geom.type == 'MultiPoint')

    @property
    def hasMultiLineString(self):
        return int(self.geom.type == 'MultiLineString')

    @property
    def hasMultiPolygon(self):
        return int(self.geom.type == 'MultiPolygon')

    @property
    def coords_kml(self):
        try:
            return coords_to_kml(self.geom)
        except:
            pass

    @property
    def properties_vocabulary_labels(self):
        terms = getUtility(IVocabularyFactory,
                    name="displaypropertiesVocab").terms
        labels = {}
        for k, v in terms:
            labels[k] = v
        return labels

    @property
    def use_custom_styles(self):
        if not self.styles:
            return False
        return self.styles.get('use_custom_styles', False)

    @property
    def linecolor(self):
        if self.styles:
            return web2kmlcolor(self.styles['linecolor'])
        return u''

    @property
    def linewidth(self):
        if self.styles:
            return self.styles['linewidth']
        return ''

    @property
    def polygoncolor(self):
        if self.styles:
            return web2kmlcolor(self.styles['polygoncolor'])
        return u''

    @property
    def marker_image(self):
        if self.styles:
            return get_marker_image(self.context, self.styles['marker_image'])
        return u''

    @property
    def marker_image_size(self):
        if self.styles:
            return self.styles['marker_image_size']
        return u''

    @property
    def item_type(self):
        if callable(self.context.portal_type):
            return self.context.portal_type()
        else:
            return self.context.portal_type

    @property
    def item_url(self):
        return self.context.absolute_url()

    def display_properties(self, document):
        properties = document.display_properties
        if self.styles and self.use_custom_styles:
            properties = self.styles['display_properties']
        return [(self.properties_vocabulary_labels.get(prop, prop),
                        self.getDisplayValue(prop)) for prop in properties
                        if getattr(self.context, prop, False)]

    def getDisplayValue(self, prop):
        return self.formatDisplayProperty(getattr(self.context, prop), prop)

    def formatDisplayProperty(self, value, prop):
        # value could be a bound method...
        try:
            value = value()
        except:
            pass

        # value could be a string representing a date
        if prop in DISPLAY_PROPERTIES_DATES:
            return self.context.toLocalizedTime(value, long_format=1)

        if isinstance(value, tuple) or\
                        isinstance(value, list):
            return ' '.join(value)

        if isinstance(value, str):
            return value

        if isinstance(value, dict):
            string = ''
            for k, v in value.items():
                string += str(k) + ': ' + str(v)
            return string
        return value

    def lead_image(self, scale='thumb', css_class="tileImage"):
        #is brain?
        try:
            obj = self.context.getObject()
        except AttributeError:
            obj = self.context

        try:
            image_field = obj.getField('image')
        except:
            return None

        if has_leadimage and not image_field:
            image_field = obj.getField(IMAGE_FIELD_NAME)

        if image_field and image_field.get_size(obj):
            return image_field.tag(obj, scale=scale, css_class=css_class)
        return None