diff options
| -rw-r--r-- | ishtar_common/static/js/ishtar-map.js | 48 | ||||
| -rw-r--r-- | ishtar_common/static/js/ishtar.js | 37 | ||||
| -rw-r--r-- | ishtar_common/views_item.py | 41 | 
3 files changed, 84 insertions, 42 deletions
| diff --git a/ishtar_common/static/js/ishtar-map.js b/ishtar_common/static/js/ishtar-map.js index 8fb01f06c..f1fbca3e1 100644 --- a/ishtar_common/static/js/ishtar-map.js +++ b/ishtar_common/static/js/ishtar-map.js @@ -1,5 +1,7 @@  /* layers */ +var default_pointer = "../media/images/default-pointer.png"; +  var source_osm = function(options){      return new ol.layer.Tile({          source: new ol.source.OSM() @@ -15,7 +17,6 @@ var get_layers = function(layers){      if (!layers){          layers = [{'type': 'osm', 'options': null}];      } -    console.log(layers);      var ol_layers = [];      for (idx in layers){          var layer_attr = layers[idx]; @@ -31,20 +32,59 @@ var get_layers = function(layers){  var get_markers = function(points){  }; +/* styles */ +var get_style = function(feature){ +    /* +    properties = feature.getProperties(); +    var size = [properties.icon_width, properties.icon_height]; +    var anchor = [properties.icon_offset_x, +                  properties.icon_offset_y]; + +    return new ol.style.Style({ +        image: new ol.style.Icon({ +            anchor: [17, 50], +            anchorXUnits: 'pixels', +            anchorYUnits: 'pixels', +            src: default_pointer, +            size: [35, 50] +        }) +    }); +    */ +    var image = new ol.style.Circle({ +        radius: 5, +        fill: null, +        stroke: new ol.style.Stroke({color: 'red', width: 1}) +    }); + +    return new ol.style.Style({ +        image: image +    }); +}; +  /* display map */  var display_map = function(map_id, points, layers){ +    console.log((new ol.format.GeoJSON()).readFeatures(points)); +    var vector_source = new ol.source.Vector({ +        features: (new ol.format.GeoJSON()).readFeatures(points) +    }); + +    var vector_layer = new ol.layer.Vector({ +        source: vector_source, +        style: get_style +    }); + +    var map_layers = get_layers(layers); +    map_layers.push(vector_layer);      var map = new ol.Map({          target: map_id, -        layers: get_layers(layers), +        layers: map_layers,          view: new ol.View({              center: ol.proj.fromLonLat([37.41, 8.82]),              zoom: 4          })      }); -    get_markers(points); -  } diff --git a/ishtar_common/static/js/ishtar.js b/ishtar_common/static/js/ishtar.js index f450fef49..96f1a4cc9 100644 --- a/ishtar_common/static/js/ishtar.js +++ b/ishtar_common/static/js/ishtar.js @@ -1241,46 +1241,11 @@ var render_map = function(data_table, table_name, nb_select, map_id){      //var html = render_paginate_select(table_name, 'map', nb_select);      var html = "";      html += "<div class='ishtar-table-map' id='" + map_id + "'></div>"; -    var table_cols = data_table["table-cols"]; - -    var points = []; -    var missings = []; -    $.each(data_table["rows"], function(idx, data){ -        var name = ''; -        if ("cached_label" in data){ -            name = data["cached_label"]; -        } -        if ("name" in data){ -            name = data["name"]; -        } - -        var point = ""; -        for (key in data){ -            if (key.endsWith('point_2d') && data[key] != "") { -                point = data[key]; -            } -        } - -        if (point){ -            points.push({ -                "id": data["id"], -                "point": point, -                "name": name -            }); -        } else { -            missings.push({ -                "id": data["id"], -                "name": name -            }); -        } -    }); -    return {"points": points, "html": html}; +    return {"points": data_table, "html": html};  };  var register_map = function(map_id, points){ -    console.log(points);      display_map(map_id, points); -    // pass  };  var main_submit_search = function(){ diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index e720a0437..f019fbcb3 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -12,7 +12,7 @@ import subprocess  from tempfile import NamedTemporaryFile  from django.conf import settings -from django.contrib.gis.geos import GEOSException +from django.contrib.gis.geos import GEOSException, GEOSGeometry  from django.contrib.staticfiles.templatetags.staticfiles import static  from django.core.cache import cache  from django.core.exceptions import ObjectDoesNotExist @@ -991,6 +991,41 @@ def _format_val(val):      return unicode(val) +def _format_geojson(rows): +    data = { +        'type': 'FeatureCollection', +        'crs': { +            'type': 'name', +            'properties': { +                'name': 'EPSG:4326' +            } +        }, +        'features': [], +        'no-geo': [] +    } +    if not rows: +        return data +    geo_attr, name_attr = None, None +    for attr in rows[0]: +        if attr.endswith('point_2d'): +            geo_attr = attr +        if attr in ['name', 'cached_label']: +            name_attr = attr +    if not geo_attr or not name_attr: +        return data +    for row in rows: +        feat = {'name': row[name_attr], 'id': row['id']} +        if not row[geo_attr]: +            data['no-geo'].append(feat) +            continue +        feature = {'type': 'Feature'} +        feature['properties'] = feat +        point = GEOSGeometry(row[geo_attr]) +        feature['geometry'] = json.loads(point.json) +        data['features'].append(feature) +    return data + +  DEFAULT_ROW_NUMBER = 10  # length is used by ajax DataTables requests  EXCLUDED_FIELDS = ['length'] @@ -1608,7 +1643,9 @@ def get_item(model, func_name, default_name, extra_request_keys=None,                      elif 'name' in res:                          res['value'] = res.pop('name')                  rows.append(res) -            if full == 'shortcut': +            if data_type == 'json-map': +                data = json.dumps(_format_geojson(rows)) +            elif full == 'shortcut':                  data = json.dumps(rows)              else:                  data = json.dumps({ | 
