def get_parks(request): """ Returns parks as JSON based search parameters """ querydict = request.GET kwargs = querydict.dict() no_map = kwargs.pop('no_map', False) # FIXME: int() will throw if this arg isn't parseable. That should be handled page = int(kwargs.pop('page', 1)) # FIXME: int() will throw if this arg isn't parseable. That should be handled # FIXME: We should figure out what a reasonable default is here. # *perhaps* we shouldn't do paging if `page` above is not specified? page_size = int(kwargs.pop('page_size', 15)) user = request.user slug = kwargs.get('slug', False) filters = kwargs try: parks_filter = Park.objects.filter(**filters) parks = parks_filter.select_related('parkowner').distinct('name') parks_pages = Paginator(parks, page_size) parks_page = parks_pages.page(page) extent = get_extent_for_openlayers(parks_filter, 26986) if no_map: parks_json = { p.pk: p.to_external_document(user, include_large=True, include_extra_info=bool(slug)) for p in parks_page } response_json = { "parks": parks_json, "pages": parks_pages.num_pages, "bbox": list(extent.coords) } else: # FIXME: should this even be paged? There's nowhere to put the total # of pages... response_json = { p.pk: p.to_external_document(user, include_large=True) for p in parks_page } return HttpResponse(json.dumps(response_json), mimetype='application/json') except Exception as e: # no content print e return HttpResponse(str(e), status=204)
def get_parks(request): """ Returns parks as JSON based search parameters """ querydict = request.GET kwargs = querydict.dict() no_map = kwargs.pop('no_map', False) # FIXME: int() will throw if this arg isn't parseable. That should be handled page = int(kwargs.pop('page', 1)) # FIXME: int() will throw if this arg isn't parseable. That should be handled # FIXME: We should figure out what a reasonable default is here. # *perhaps* we shouldn't do paging if `page` above is not specified? page_size = int(kwargs.pop('page_size', 15)) user = request.user slug = kwargs.get('slug', False) filters = kwargs try: parks_filter = Park.objects.filter(**filters) parks = parks_filter.select_related('parkowner').distinct('name') parks_pages = Paginator(parks, page_size) parks_page = parks_pages.page(page) extent = get_extent_for_openlayers(parks_filter, 26986) if no_map: parks_json = {p.pk: p.to_external_document(user, include_large=True, include_extra_info=bool(slug)) for p in parks_page} response_json = { "parks": parks_json, "pages": parks_pages.num_pages, "bbox": list(extent.coords) } else: # FIXME: should this even be paged? There's nowhere to put the total # of pages... response_json = {p.pk: p.to_external_document(user, include_large=True) for p in parks_page} return HttpResponse(json.dumps(response_json), mimetype='application/json') except Exception as e: # no content print e return HttpResponse(str(e), status=204)
def get_parks(): cursor = connection.cursor() parks_needing_images = Park.objects.annotate(num_photos=Count("images")).filter(num_photos__lt=MIN_PICTURE_COUNT) query_extent = get_extent_for_openlayers(parks_needing_images, 26986) print query_extent nrow = int((abs(query_extent[1][1]) - abs(query_extent.coords[0][1])) / BBOW_H) ncol = int((abs(query_extent[0][0]) - abs(query_extent.coords[1][0])) / BBOX_W) ### fishnet function query = """CREATE OR REPLACE FUNCTION ST_CreateFishnet( nrow integer, ncol integer, xsize float8, ysize float8, x0 float8 DEFAULT 0, y0 float8 DEFAULT 0, OUT "row" integer, OUT col integer, OUT geom geometry) RETURNS SETOF record AS $$ SELECT i + 1 AS row, j + 1 AS col, ST_Translate(cell, j * $3 + $5, i * $4 + $6) AS geom FROM generate_series(0, $1 - 1) AS i, generate_series(0, $2 - 1) AS j, ( SELECT ('POLYGON((0 0, 0 '||$4||', '||$3||' '||$4||', '||$3||' 0,0 0))')::geometry AS cell ) AS foo; $$ LANGUAGE sql IMMUTABLE STRICT; SELECT bbox FROM (SELECT ST_Transform(ST_SetSRID(cells.geom,4326),26986) AS bbox FROM ST_CreateFishnet({0}, {1}, {2},{3}, {4}, {5}) AS cells) AS results, parks_park """.format( nrow, ncol, BBOX_W, BBOW_H, query_extent.coords[0][0], query_extent.coords[0][1] ) print query cursor.execute(query) for row in cursor.fetchall(): print row
def get_parks(): cursor = connection.cursor() parks_needing_images = Park.objects.annotate(num_photos=Count('images')).filter(num_photos__lt=MIN_PICTURE_COUNT) query_extent = get_extent_for_openlayers(parks_needing_images, 26986) print query_extent nrow = int((abs(query_extent[1][1]) - abs(query_extent.coords[0][1]))/BBOW_H) ncol = int((abs(query_extent[0][0]) - abs(query_extent.coords[1][0]))/BBOX_W) ### fishnet function query = """CREATE OR REPLACE FUNCTION ST_CreateFishnet( nrow integer, ncol integer, xsize float8, ysize float8, x0 float8 DEFAULT 0, y0 float8 DEFAULT 0, OUT "row" integer, OUT col integer, OUT geom geometry) RETURNS SETOF record AS $$ SELECT i + 1 AS row, j + 1 AS col, ST_Translate(cell, j * $3 + $5, i * $4 + $6) AS geom FROM generate_series(0, $1 - 1) AS i, generate_series(0, $2 - 1) AS j, ( SELECT ('POLYGON((0 0, 0 '||$4||', '||$3||' '||$4||', '||$3||' 0,0 0))')::geometry AS cell ) AS foo; $$ LANGUAGE sql IMMUTABLE STRICT; SELECT bbox FROM (SELECT ST_Transform(ST_SetSRID(cells.geom,4326),26986) AS bbox FROM ST_CreateFishnet({0}, {1}, {2},{3}, {4}, {5}) AS cells) AS results, parks_park """.format(nrow,ncol,BBOX_W, BBOW_H, query_extent.coords[0][0],query_extent.coords[0][1]) print query cursor.execute(query); for row in cursor.fetchall(): print row