Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

# Create your views here. 

from madrona.heatmap.models import create_heatmap 

from madrona.shapes.views import ShpResponder 

from madrona.common import default_mimetypes as mimetypes 

from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden, Http404 

from madrona.features import get_feature_by_uid, get_collection_models 

from madrona.features.models import PolygonFeature 

import os 

 

def overlap_geotiff(collection_uids, user=None): 

    collections = [get_feature_by_uid(x) for x in collection_uids.split(',')] 

    collections = [x for x in collections if x.__class__ in get_collection_models()] 

    if len(collections) < 1: 

        raise Http404 

 

    filenames = [] 

    for collection in collections: 

        viewable, response = collection.is_viewable(user) 

        if user and not viewable: 

            return response 

 

        fs = collection.feature_set(recurse=True) 

        poly_fs = [x for x in fs if isinstance(x,PolygonFeature)] 

        unique_types = list(set([x.__class__ for x in poly_fs])) 

        for model in unique_types: 

            responder = ShpResponder(model.objects.filter(pk__in=[x.pk for x in poly_fs if x.__class__ == model])) 

            responder.geo_field = 'geometry_final' 

            fn = responder('return_file_not_response') 

            filenames.append(fn) 

 

    temp_geotiff = create_heatmap(filenames) 

    return temp_geotiff 

 

def overlap_geotiff_response(request, collection_uids): 

    import cStringIO 

    buff = cStringIO.StringIO() 

    temp_geotiff = overlap_geotiff(collection_uids, request.user) 

    if isinstance(temp_geotiff, HttpResponse): 

        return temp_geotiff 

    rfile = open(temp_geotiff,'rb') 

    buff.write(rfile.read()) 

    buff.flush() 

    stream = buff.getvalue() 

    response = HttpResponse() 

    response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(temp_geotiff) 

    response['Content-length'] = str(len(stream)) 

    response['Content-Type'] = 'image/geotiffint16' 

    response.write(stream) 

    return response 

 

def overlap_kmz_response(request, collection_uids): 

    import cStringIO 

    import shutil 

    import tempfile 

 

    buff = cStringIO.StringIO() 

    temp_geotiff = overlap_geotiff(collection_uids, request.user) 

    if isinstance(temp_geotiff, HttpResponse): 

        return response 

 

    tempdir = os.path.join(tempfile.gettempdir(),str(collection_uids.__hash__())) 

    if os.path.exists(tempdir): 

        shutil.rmtree(tempdir) 

    os.mkdir(tempdir) 

    outrgb = os.path.join(tempdir, "rgb.png") 

    outvrt = os.path.join(tempdir, "rgb.vrt") 

    outkmldir = os.path.join(tempdir, "kmlfiles") 

    if os.path.exists(outkmldir): 

        shutil.rmtree(outkmldir) 

    os.mkdir(outkmldir) 

    outkmz = os.path.join(tempdir, "array_overlap.kmz") 

 

    # Convert to RGB  

    cmd = "pct2rgb.py -of PNG %s %s" % (temp_geotiff, outrgb) 

    print cmd 

    print os.popen(cmd).read() 

 

    # Assign a nodata value 

    cmd = "gdal_translate -of vrt -a_nodata 0 %s %s" % (outrgb, outvrt) 

    print cmd 

    print os.popen(cmd).read() 

 

    # Split into wgs84 tiles 

    cmd = "gdal2tiles.py -k -z 6-10 -p geodetic %s %s" % (outvrt, outkmldir) 

    print cmd 

    print os.popen(cmd).read() 

 

    # Zip up directory into a kmz 

    print "Creating", outkmz 

    from madrona.common.utils import KMZUtil 

    zu = KMZUtil() 

    filename = outkmz 

    os.chdir(outkmldir) 

    directory = "" 

    zu.toZip(directory, filename) 

 

    if not os.path.exists(outkmz): 

        return HttpResponse("KMZ creation error", status=500) 

    rfile = open(outkmz,'rb') 

    buff.write(rfile.read()) 

    buff.flush() 

    stream = buff.getvalue() 

    response = HttpResponse() 

    response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(outkmz) 

    response['Content-length'] = str(len(stream)) 

    response['Content-Type'] = mimetypes.KMZ 

    response.write(stream) 

    return response