Last active
August 29, 2015 14:15
-
-
Save plablo09/8bc9ecb016a66ebee40d to your computer and use it in GitHub Desktop.
Filtrar mallas por geometría
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from pylons import request, response, session, tmpl_context as c | |
from pylons.controllers.util import abort, redirect | |
from mapfishapp.lib.base import BaseController | |
from mapfishapp.model.mallas import Malla | |
from mapfishapp.model.meta import Session | |
from mapfishapp.model import meta | |
from shapely.wkt import loads | |
from mapfish.protocol import Protocol, create_default_filter | |
from mapfish.decorators import geojsonify | |
from geojson import Feature, FeatureCollection | |
class MallasController(BaseController): | |
readonly = False # if set to True, only GET is supported | |
def __init__(self): | |
self.protocol = Protocol(Session, Malla, self.readonly) | |
@geojsonify | |
def index(self, format='json'): | |
"""GET /: return all features.""" | |
# If no filter argument is passed to the protocol index method | |
# then the default MapFish filter is used. | |
# | |
# If you need your own filter with application-specific params | |
# taken into acount, create your own filter and pass it to the | |
# protocol read method. | |
# | |
# E.g. | |
# | |
# from sqlalchemy.sql import and_ | |
# | |
# default_filter = create_default_filter(request, Malla) | |
# filter = and_(default_filter, Malla.columname.ilike('%value%')) | |
# return self.protocol.read(request, filter=filter) | |
if format != 'json': | |
abort(404) | |
return self.protocol.read(request) | |
@geojsonify | |
def filter_malla(self): | |
""" | |
Recibe una geometría y parámetros adicionales de búsqueda. | |
Regresa la geometría de los rectángulos de la malla que intersectan | |
con la geometría recibida y cumplen con las condiciones especificadas. | |
""" | |
filterGeom = request.params.get('geometry', 'Not present') | |
if filterGeom == 'Not present': | |
return 'todomal' | |
else: | |
mallas_q = meta.Session.query(Malla).filter(Malla.geom.intersects(filterGeom)) #creas el objeto query | |
mallasFiltered = mallas_q.order_by(Malla.fecha.desc()).limit(10) | |
mallasJson = [Feature(id=int(malla.gid),geometry=loads(Session.scalar(malla.geom.wkt)), | |
properties={"scene_id": malla.scene_id ,'path': malla.path, | |
'row': malla.row}) for malla in mallasFiltered] | |
return FeatureCollection(mallasJson) | |
# @geojsonify | |
# def show(self, id, format='json'): | |
# """GET /id: Show a specific feature.""" | |
# if format != 'json': | |
# abort(404) | |
# return self.protocol.read(request, response, id=id) | |
# @geojsonify | |
# def create(self): | |
# """POST /: Create a new feature.""" | |
# return self.protocol.create(request, response) | |
# @geojsonify | |
# def update(self, id): | |
# """PUT /id: Update an existing feature.""" | |
# return self.protocol.update(request, response, id) | |
# def delete(self, id): | |
# """DELETE /id: Delete an existing feature.""" | |
# return self.protocol.delete(request, response, id) | |
# def count(self): | |
# """GET /count: Count all features.""" | |
# return self.protocol.count(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment