<commit_before>from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden, Http404
from django.template import RequestContext
from django.shortcuts import get_object_or_404, render_to_response
from django.db import connection
from lingcod.common import default_mimetypes as mimetypes
from lingcod.common import utils
from lingcod.mpa.models import MpaDesignation
from lingcod.sharing.utils import can_user_view
import mapnik
import settings
from lingcod.staticmap.models import MapConfig

mpa_class = utils.get_mpa_class()

def get_mpas(request):
    # get a list of the MPA ids to display
    mpas = []
    if 'mpas' in request.REQUEST: 
        mpas = str(request.REQUEST['mpas']).split(',')
        # make sure all given mpas are integers
        mpas = [int(x) for x in mpas if x.isdigit()]

    # If an array is specified, it's mpas are appended to the mpa list
    if 'array' in request.REQUEST:
        input_array_id = request.REQUEST['array']
        # Find that array object and get all associated mpas
        MpaArray = utils.get_array_class()
        try: 
            the_array = MpaArray.objects.get(id=input_array_id)
        except:
            raise Http404

        mpaids = [x.id for x in the_array.mpa_set]
        for mpaid in mpaids:
            mpas.append(mpaid)
            
    return mpas

def auto_extent(mpa_ids,srid=4326):
    if not srid:
        srid = 4326 # Assume latlong if none
    # would be nice to just do a transform().extent() but this doesnt work - geodjango bug
    ugeom = mpa_class.objects.filter(pk__in=mpa_ids).unionagg().transform(srid,clone=True)
    bbox = ugeom.extent
    width = bbox[2]-bbox[0]
    height = bbox[3]-bbox[1]
    buffer = 0.15
    return bbox[0]-width*buffer, bbox[1]-height*buffer, bbox[2]+width*buffer, bbox[3]+height*buffer

def get_mpa_filter_string(mpas):
    if len(mpas) < 1:
        mpas = [0]
    mpa_queries = ['[id] = %d' % x for x in mpas] 
    mpa_filter_string = " or ".join(mpa_queries)
    return mpa_filter_string

def process_mapfile_text(mapfile, mpas):
    # Replace local settings-specific variables using keywords in mapnik mapfile
    # Uses ALL_CAPS keywords in the mapfile
    xmltext = open(mapfile).read()
<commit_msg>minor change to buffer extent of autozoom in staticmap<commit_after>from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden, Http404
from django.template import RequestContext
from django.shortcuts import get_object_or_404, render_to_response
from django.db import connection
from lingcod.common import default_mimetypes as mimetypes
from lingcod.common import utils
from lingcod.mpa.models import MpaDesignation
from lingcod.sharing.utils import can_user_view
import mapnik
import settings
from lingcod.staticmap.models import MapConfig

mpa_class = utils.get_mpa_class()

def get_mpas(request):
    # get a list of the MPA ids to display
    mpas = []
    if 'mpas' in request.REQUEST: 
        mpas = str(request.REQUEST['mpas']).split(',')
        # make sure all given mpas are integers
        mpas = [int(x) for x in mpas if x.isdigit()]

    # If an array is specified, it's mpas are appended to the mpa list
    if 'array' in request.REQUEST:
        input_array_id = request.REQUEST['array']
        # Find that array object and get all associated mpas
        MpaArray = utils.get_array_class()
        try: 
            the_array = MpaArray.objects.get(id=input_array_id)
        except:
            raise Http404

        mpaids = [x.id for x in the_array.mpa_set]
        for mpaid in mpaids:
            mpas.append(mpaid)
            
    return mpas

#i suspect we could drop the conditional since we have srid assigned in the parameter list
#also, we might want to use equivalent settings srid variable instead of hardcoding 4326    
def auto_extent(mpa_ids,srid=4326):
    if not srid:
        srid = 4326 # Assume latlong if none
    # would be nice to just do a transform().extent() but this doesnt work - geodjango bug
    ugeom = mpa_class.objects.filter(pk__in=mpa_ids).unionagg().transform(srid,clone=True)
    bbox = ugeom.extent
    width = bbox[2]-bbox[0]
    height = bbox[3]-bbox[1]
    buffer = 0.20 #might add a settings variable for this?
    return bbox[0]-width*buffer, bbox[1]-height*buffer, bbox[2]+width*buffer, bbox[3]+height*buffer

def get_mpa_filter_string(mpas):
    if len(mpas) < 1:
        mpas = [0]
    mpa_queries = ['[id] = %d' % x for x in mpas] 
    mpa_filter_string = " or ".join(mpa_queries)
    return mpa_filter_string

def process_mapfile_text(mapfile, mpas):
    # Replace local settings-specific variables using keywords in mapnik mapfile
    # Uses ALL_CAPS keywords in the mapfile
    xmltext = open(mapfile).read()

{'fraction_non_alphanumeric': 0.08243801652892561, 'fraction_numerical': 0.013223140495867768, 'mean_word_length': 3.416970802919708, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4792812', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1544, 'n_words': 469}