Ejemplo n.º 1
0
def view_classify_shapefile(request, worldmap_layerinfo, first_time_notify=False):
    """Called by 'view_shapefile' -- no direct url associated with this view"""

    assert isinstance(worldmap_layerinfo, WorldMapLayerInfo),\
        "worldmap_layerinfo must be an instance of WorldMapLayerInfo"

    d = get_common_lookup(request)
    d['page_title'] = PANEL_TITLE_STYLE_MAP
    d[GEOCONNECT_STEP_KEY] = STEP2_STYLE

    classify_form = ClassifyLayerForm(**worldmap_layerinfo.get_dict_for_classify_form())

    shapefile_info = worldmap_layerinfo.get_gis_data_info()
    if not shapefile_info:
        raise Http404('shapefile md5 not found!')

    delete_form = DeleteMapForm.get_form_with_initial_vals(worldmap_layerinfo)

    d.update(worldmap_layerinfo.get_core_data_dict_for_views())

    d['gis_data_info'] = shapefile_info
    #d['shapefile_info'] = shapefile_info
    d['classify_form'] = classify_form
    d['delete_form'] = delete_form

    d['ATTRIBUTE_VALUE_DELIMITER'] = ATTRIBUTE_VALUE_DELIMITER
    d['first_time_notify'] = first_time_notify
    d['SELECT_LABEL'] = SELECT_LABEL

    return render(request, 'shapefiles/main_outline_shp.html', d)
Ejemplo n.º 2
0
def build_map_html(request, worldmap_info):
    """
    Expects a WorldMapLayerInfo object

    Create 2 HTML strings:
        1 - map_html
            - Completed map via iframe
            - Download links using Geoserver functions
            - Attribute table
        2 - user_message_html
            - User message about join
    """
    if not isinstance(worldmap_info, WorldMapLayerInfo):
        err_msg = ('worldmap_info needs to be a WorldMapLayerInfo'
                   ' object. Not type: %s')\
                   % (type(worldmap_info))
        LOGGER.error(err_msg)
        return None, None

    delete_form = DeleteMapForm.get_form_with_initial_vals(worldmap_info)

    template_dict = get_common_lookup(request)

    failed_records_list = worldmap_info.get_failed_rows()
    num_failed_download_records = min(MAX_FAILED_ROWS_TO_BUILD,\
                            worldmap_info.get_unmapped_record_count())

    template_dict.update(
        dict(worldmap_layerinfo=worldmap_info,
             INITIAL_SELECT_CHOICE=INITIAL_SELECT_CHOICE,
             SELECT_LABEL=SELECT_LABEL,
             core_data=worldmap_info.core_data,
             gis_data_info=worldmap_info.get_gis_data_info(),
             download_links=worldmap_info.get_formatted_download_links(),
             attribute_data=worldmap_info.attribute_data,
             failed_records_list=failed_records_list,
             num_failed_download_records=num_failed_download_records,
             delete_form=delete_form,
             page_title=PANEL_TITLE_STYLE_MAP))

    # --------------------------------
    # Classification form attributes
    # --------------------------------
    classify_form = ClassifyLayerForm(\
            **worldmap_info.get_dict_for_classify_form())

    classify_params = {\
            GEOCONNECT_STEP_KEY : STEP2_STYLE,\
            'ATTRIBUTE_VALUE_DELIMITER' : ATTRIBUTE_VALUE_DELIMITER,\
            'classify_form' : classify_form}

    template_dict.update(classify_params)

    map_html = render_to_string('worldmap_layers/map_with_classify.html',
                                template_dict, request)

    user_message_html = render_to_string(
        'worldmap_layers/new_map_message.html', template_dict, request)

    return (map_html, user_message_html)
Ejemplo n.º 3
0
def view_existing_map(request, worldmap_info=None):
    """
    Test view a WorldMapTabularLayerInfo object
    """
    if not (isinstance(worldmap_info, WorldMapJoinLayerInfo) or\
        isinstance(worldmap_info, WorldMapLatLngInfo)):
        LOGGER.error(
            'worldmap_info needs to be a WorldMapJoinLayerInfo\
         or WorldMapLatLngInfo object. Not: %s', worldmap_info)
        return HttpResponse('Sorry! No WorldMap information was found.')

    template_dict = get_common_lookup(request)

    map_html, user_message_html = build_map_html(request, worldmap_info)
    if map_html is None:
        LOGGER.error("Failed to create map HTML using worldmap_info: %s (%d)" %\
            worldmap_info, worldmap_info.id)
        user_msg = 'Sorry! Failed to create map. Please try again. (code:ve3)'
        return HttpResponse(user_msg)

    template_dict = dict(
        worldmap_layerinfo=worldmap_info,
        attribute_data=worldmap_info.attribute_data,
        tabular_map_div=map_html,
        user_message_html=user_message_html,  # not used for existing maps
        gis_data_info=worldmap_info.get_gis_data_info(),
        test_files=TabularFileInfo.objects.all(),
        page_title=PANEL_TITLE_STYLE_MAP,
    )

    template_dict[GEOCONNECT_STEP_KEY] = STEP2_STYLE
    template_dict['GEOCONNECT_STEPS'] = GEOCONNECT_STEPS

    return render(request, 'tabular_files/main_outline_tab.html',
                  template_dict)
Ejemplo n.º 4
0
def view_existing_map(request, worldmap_info=None):
    """
    View a WorldMapTabularLayerInfo object
    """
    if not (isinstance(worldmap_info, WorldMapJoinLayerInfo) or\
        isinstance(worldmap_info, WorldMapLatLngInfo)):
        LOGGER.error('worldmap_info needs to be a WorldMapJoinLayerInfo\
         or WorldMapLatLngInfo object. Not: %s', worldmap_info)
        return HttpResponse('Sorry! No WorldMap information was found.')

    template_dict = get_common_lookup(request)

    map_html, user_message_html = build_map_html(request, worldmap_info)
    if map_html is None:
        LOGGER.error("Failed to create map HTML using worldmap_info: %s (%d)" %\
            worldmap_info, worldmap_info.id)
        user_msg = 'Sorry! Failed to create map. Please try again. (code:ve3)'
        return HttpResponse(user_msg)


    template_dict.update(\
        dict(worldmap_layerinfo=worldmap_info,
        attribute_data=worldmap_info.attribute_data,
        tabular_map_div=map_html,
        user_message_html=user_message_html,    # not used for existing maps
        gis_data_info=worldmap_info.get_gis_data_info(),
        test_files=TabularFileInfo.objects.all(),
        page_title=PANEL_TITLE_STYLE_MAP))

    template_dict[GEOCONNECT_STEP_KEY] = STEP2_STYLE
    template_dict['GEOCONNECT_STEPS'] = GEOCONNECT_STEPS

    return render(request, 'tabular_files/main_outline_tab.html', template_dict)
Ejemplo n.º 5
0
def view_mapit_incoming_token64(request, dataverse_token):
    """
    (1) Check incoming url for a callback key 'cb'
        and use the callback url to retrieve the DataverseInfo via a POST
    (2) Route the request depending on the type of data returned
    """
    maintenance_notice = get_maintenance_snippet()
    if maintenance_notice is not None:
        info_dict = get_common_lookup(request)
        info_dict['maintenance_notice'] = maintenance_notice
        return render(request, 'content_pages/view_home.html', info_dict)

    # (1) Check incoming url for a callback url
    # and use the url to retrieve the DataverseInfo via a POST
    #
    request_helper = InitialRequestHelper(request, dataverse_token)
    if request_helper.has_err:
        return view_formatted_error_page(request,\
                            request_helper.err_type,\
                            request_helper.err_msg)

    # (2) Route the request depending on the type of data returned
    #
    mapping_type = request_helper.mapping_type

    #  Is the mapping type valid?
    #  Knowingly redundant, also checked in requestHelper
    #
    if not is_valid_dv_type(mapping_type):
        err_msg = 'The mapping_type for this metadata was not valid.  Found: %s' % mapping_type

        return view_formatted_error_page(request,\
                            FAILED_TO_IDENTIFY_METADATA_MAPPING_TYPE,\
                            err_msg)

    #  Is the mapping type active?
    #
    if not is_setting_active(mapping_type):
        return view_filetype_note_by_name(request, mapping_type)

    # Let's route it!
    #
    if is_dv_type_shapefile(mapping_type):

        return process_shapefile_info(request,\
                            request_helper.dataverse_token,\
                            request_helper.dv_data_dict)

    elif is_dv_type_tabular(mapping_type):

        return process_tabular_file_info(request,\
                            request_helper.dataverse_token,\
                            request_helper.dv_data_dict)

    elif is_dv_type_geotiff(mapping_type):

        err_msg = 'Sorry! GeoTiff mapping is currently not available'
        return view_formatted_error_page(None, err_msg)

    return HttpResponse('Error!!  Should never reach this line!')
Ejemplo n.º 6
0
def view_classify_shapefile(request,
                            worldmap_layerinfo,
                            first_time_notify=False):
    """Called by 'view_shapefile' -- no direct url associated with this view"""

    assert isinstance(worldmap_layerinfo, WorldMapLayerInfo),\
        "worldmap_layerinfo must be an instance of WorldMapLayerInfo"

    d = get_common_lookup(request)
    d['page_title'] = PANEL_TITLE_STYLE_MAP
    d[GEOCONNECT_STEP_KEY] = STEP2_STYLE

    classify_form = ClassifyLayerForm(
        **worldmap_layerinfo.get_dict_for_classify_form())

    shapefile_info = worldmap_layerinfo.get_gis_data_info()
    if not shapefile_info:
        raise Http404('shapefile md5 not found!')

    delete_form = DeleteMapForm.get_form_with_initial_vals(worldmap_layerinfo)

    d.update(worldmap_layerinfo.get_core_data_dict_for_views())

    d['gis_data_info'] = shapefile_info
    #d['shapefile_info'] = shapefile_info
    d['classify_form'] = classify_form
    d['delete_form'] = delete_form

    d['ATTRIBUTE_VALUE_DELIMITER'] = ATTRIBUTE_VALUE_DELIMITER
    d['first_time_notify'] = first_time_notify
    d['SELECT_LABEL'] = SELECT_LABEL

    return render(request, 'shapefiles/main_outline_shp.html', d)
Ejemplo n.º 7
0
def view_home(request):
    """
    Display a list of ShapefileInfo objects, each linked to a detail page.
    For testing, allow the upload of a new shapefile object.
    """
    info_dict = get_common_lookup(request)

    info_dict['page_title'] = 'Geoconnect: Dataverse <--> WorldMap'
    info_dict['maintenance_notice'] = get_maintenance_snippet()

    return render(request, 'content_pages/view_home.html', info_dict)
Ejemplo n.º 8
0
def view_home(request):
    """
    Display a list of ShapefileInfo objects, each linked to a detail page.
    For testing, allow the upload of a new shapefile object.
    """
    info_dict = get_common_lookup(request)

    info_dict['page_title'] = 'Geoconnect: Dataverse <--> WorldMap'
    info_dict['maintenance_notice'] = get_maintenance_snippet()

    return render(request, 'content_pages/view_home.html', info_dict)
Ejemplo n.º 9
0
def view_formatted_error_page(request, error_type, err_msg=None):
    """Show an error page"""

    d = get_common_lookup(request)
    d['page_title'] = 'Examine Shapefile'
    d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL
    d[GEOCONNECT_STEP_KEY] = STEP1_EXAMINE

    d['Err_Found'] = True
    if error_type is not None:
        d[error_type] = True
    d['Dataverse_Connect_Err_Msg'] = err_msg
    d['SELECT_LABEL'] = SELECT_LABEL

    return render(request, 'shapefiles/main_outline_shp.html', d)
Ejemplo n.º 10
0
def view_formatted_error_page(request, error_type, err_msg=None):
    """Show an error page"""

    d = get_common_lookup(request)
    d['page_title'] = 'Examine Shapefile'
    d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL
    d[GEOCONNECT_STEP_KEY] = STEP1_EXAMINE

    d['Err_Found'] = True
    if error_type is not None:
        d[error_type] = True
    d['Dataverse_Connect_Err_Msg'] = err_msg
    d['SELECT_LABEL'] = SELECT_LABEL

    return render(request, 'shapefiles/main_outline_shp.html', d)
Ejemplo n.º 11
0
def show_jointarget_info(request):
    """Display the latest Join Targets retrieved from the WorldMap"""

    target_info_list = None
    target_info_pretty = None

    jt_info = JoinTargetInformation.objects.first()
    if jt_info:
        target_info_list_unsorted = jt_info.target_info.get('data')

        # sort the info by type
        target_info_list = sorted(\
                        target_info_list_unsorted,
                        key=lambda k: k['geocode_type'])

        target_info_pretty = json.dumps(target_info_list, indent=4)

    info_dict = get_common_lookup(request)
    info_dict['target_info_list'] = target_info_list
    info_dict['target_info_pretty'] = target_info_pretty

    return render(request, 'show_jointarget_info.html', info_dict)
Ejemplo n.º 12
0
def view_delete_tabular_map(request):
    """
    Attempt to delete a dataverse-created WorldMap layer
    """
    if not request.POST:
        raise Http404('Delete Not Found.')

    d = get_common_lookup(request)
    d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL
    d['DATAVERSE_SERVER_URL'] = settings.DATAVERSE_SERVER_URL

    d['page_title'] = PANEL_TITLE_DELETE_MAP
    d['IS_DELETE_PAGE'] = True
    # Check the delete request
    f = DeleteMapForm(request.POST)

    if not f.is_valid():
        d['ERROR_FOUND'] = True
        d['FAILED_TO_VALIDATE'] = True
        return render(request, 'worldmap_layers/view_delete_layer.html', d)

    # Form params look good
    worldmap_layer_info = f.get_worldmap_layer_info()
    if not worldmap_layer_info:
        raise Http404('WorldMap Layer info no longer available')

    # depending on the type: tabular_info, shapefile_info, etc
    #
    if worldmap_layer_info.is_shapefile_layer():
        d['is_shapefile_layer'] = True
    else:
        d['is_tabular_layer'] = True

    gis_data_info = worldmap_layer_info.get_gis_data_info()

    d['gis_data_info'] = gis_data_info

    # -----------------------------------
    # Delete map from WorldMap
    # -----------------------------------
    flag_delete_local_worldmap_info = False

    (success, err_msg_or_None) = delete_map_layer(gis_data_info,
                                                  worldmap_layer_info)
    if success is False:
        LOGGER.error("Failed to delete WORLDMAP layer: %s", err_msg_or_None)

        if err_msg_or_None and err_msg_or_None.find(
                '"Existing layer not found."') > -1:
            pass
        else:
            d['ERROR_FOUND'] = True
            d['WORLDMAP_DATA_DELETE_FAILURE'] = True
            d['ERR_MSG'] = err_msg_or_None
            return render(request, 'worldmap_layers/view_delete_layer.html', d)
    else:
        # At this point, the layer no longer exists on WorldMap,
        # set a flag to delete it from geoconnect, even if the Dataverse
        # delete fails
        flag_delete_local_worldmap_info = True

    # -----------------------------------
    # Delete metadata from dataverse
    # -----------------------------------

    (success2, err_msg_or_None2
     ) = MetadataUpdater.delete_dataverse_map_metadata(worldmap_layer_info)

    # Delete the Geoconnect WorldMap info -- regardless of
    # whether the data was removed from Dataverse
    if flag_delete_local_worldmap_info:
        msgt('Delete worldmap_layer_info: %s' % worldmap_layer_info)
        worldmap_layer_info.delete()

    if success2 is False:
        LOGGER.error("Failed to delete Map Metadata from Dataverse: %s",
                     err_msg_or_None)

        d['ERROR_FOUND'] = True
        d['DATAVERSE_DATA_DELETE_FAILURE'] = True
        d['ERR_MSG'] = err_msg_or_None2

        return render(request, 'worldmap_layers/view_delete_layer.html', d)

    d['DELETE_SUCCESS'] = True
    d['page_title'] = PANEL_TITLE_REMAP

    return render(request, 'worldmap_layers/view_delete_layer.html', d)
Ejemplo n.º 13
0
def view_delete_tabular_map(request):
    """
    Attempt to delete a dataverse-created WorldMap layer
    """
    if not request.POST:
        raise Http404('Delete Not Found.')

    d = get_common_lookup(request)
    d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL
    d['DATAVERSE_SERVER_URL'] = settings.DATAVERSE_SERVER_URL

    d['page_title'] = PANEL_TITLE_DELETE_MAP
    d['IS_DELETE_PAGE'] = True
    # Check the delete request
    f = DeleteMapForm(request.POST)

    if not f.is_valid():
        d['ERROR_FOUND'] = True
        d['FAILED_TO_VALIDATE'] = True
        return render(request, 'worldmap_layers/view_delete_layer.html', d)

    # Form params look good
    worldmap_layer_info = f.get_worldmap_layer_info()
    if not worldmap_layer_info:
        raise Http404('WorldMap Layer info no longer available')

    # depending on the type: tabular_info, shapefile_info, etc
    #
    if worldmap_layer_info.is_shapefile_layer():
        d['is_shapefile_layer'] = True
    else:
        d['is_tabular_layer'] = True

    gis_data_info = worldmap_layer_info.get_gis_data_info()

    d['gis_data_info'] = gis_data_info

    # -----------------------------------
    # Delete map from WorldMap
    # -----------------------------------
    flag_delete_local_worldmap_info = False

    (success, err_msg_or_None) = delete_map_layer(gis_data_info, worldmap_layer_info)
    if success is False:
        LOGGER.error("Failed to delete WORLDMAP layer: %s", err_msg_or_None)

        if err_msg_or_None and err_msg_or_None.find('"Existing layer not found."') > -1:
            pass
        else:
            d['ERROR_FOUND'] = True
            d['WORLDMAP_DATA_DELETE_FAILURE'] = True
            d['ERR_MSG'] = err_msg_or_None
            return render(request, 'worldmap_layers/view_delete_layer.html', d)
    else:
        # At this point, the layer no longer exists on WorldMap,
        # set a flag to delete it from geoconnect, even if the Dataverse
        # delete fails
        flag_delete_local_worldmap_info = True

    # -----------------------------------
    # Delete metadata from dataverse
    # -----------------------------------

    (success2, err_msg_or_None2) = MetadataUpdater.delete_dataverse_map_metadata(worldmap_layer_info)

    # Delete the Geoconnect WorldMap info -- regardless of
    # whether the data was removed from Dataverse
    if flag_delete_local_worldmap_info:
        msgt('Delete worldmap_layer_info: %s' % worldmap_layer_info)
        worldmap_layer_info.delete()

    if success2 is False:
        LOGGER.error("Failed to delete Map Metadata from Dataverse: %s", err_msg_or_None)

        d['ERROR_FOUND'] = True
        d['DATAVERSE_DATA_DELETE_FAILURE'] = True
        d['ERR_MSG'] = err_msg_or_None2

        return render(request, 'worldmap_layers/view_delete_layer.html', d)

    d['DELETE_SUCCESS'] = True
    d['page_title'] = PANEL_TITLE_REMAP

    return render(request, 'worldmap_layers/view_delete_layer.html', d)
Ejemplo n.º 14
0
def build_map_html(request, worldmap_info):
    """
    Expects a WorldMapLayerInfo object

    Create 2 HTML strings:
        1 - map_html
            - Completed map via iframe
            - Download links using Geoserver functions
            - Attribute table
        2 - user_message_html
            - User message about join
    """
    if not isinstance(worldmap_info, WorldMapLayerInfo):
        err_msg = ('worldmap_info needs to be a WorldMapLayerInfo'
                   ' object. Not type: %s')\
                   % (type(worldmap_info))
        LOGGER.error(err_msg)
        return None, None

    delete_form = DeleteMapForm.get_form_with_initial_vals(worldmap_info)

    template_dict = get_common_lookup(request)

    failed_records_list = worldmap_info.get_failed_rows()
    num_failed_download_records = min(MAX_FAILED_ROWS_TO_BUILD,\
                            worldmap_info.get_unmapped_record_count())


    template_dict.update(dict(\
            worldmap_layerinfo=worldmap_info,
            INITIAL_SELECT_CHOICE=INITIAL_SELECT_CHOICE,
            SELECT_LABEL=SELECT_LABEL,
            core_data=worldmap_info.core_data,
            gis_data_info=worldmap_info.get_gis_data_info(),
            download_links=worldmap_info.get_formatted_download_links(),
            attribute_data=worldmap_info.attribute_data,
            failed_records_list=failed_records_list,
            num_failed_download_records=num_failed_download_records,
            delete_form=delete_form,
            page_title=PANEL_TITLE_STYLE_MAP))

    # --------------------------------
    # Classification form attributes
    # --------------------------------
    classify_form = ClassifyLayerForm(\
            **worldmap_info.get_dict_for_classify_form())

    classify_params = {\
            GEOCONNECT_STEP_KEY : STEP2_STYLE,\
            'ATTRIBUTE_VALUE_DELIMITER' : ATTRIBUTE_VALUE_DELIMITER,\
            'classify_form' : classify_form}

    template_dict.update(classify_params)

    map_html = render_to_string('worldmap_layers/map_with_classify.html',
                                template_dict,
                                request)

    user_message_html = render_to_string('worldmap_layers/new_map_message.html',
                                         template_dict,
                                         request)

    return (map_html, user_message_html)
Ejemplo n.º 15
0
def view_tabular_file(request, tab_md5):
    """
    View tabular file that has been sent over via Dataverse
    """
    # ----------------------------------
    # Retrieve the Tabular file information
    # ----------------------------------
    try:
        tabular_info = TabularFileInfo.objects.get(md5=tab_md5)
    except TabularFileInfo.DoesNotExist:
        raise Http404('No TabularFileInfo for md5: %s' % tab_md5)

    # ----------------------------------
    # SKIP THIS -> LOOK FOR LAYER IN WORLDMAP EACH TIME
    # Does the file already have an associated WorldMap layer
    # ----------------------------------
    # Getting rid of this -> Fetch new info each time
    #worldmap_tabularinfo = tabular_info.get_worldmap_info()
    #if worldmap_tabularinfo is not None:
    #    # A map exists: show it!
    #    return view_existing_map(request, worldmap_tabularinfo)

    #
    # Is there a WorldMap layer but Geoconnect doesn't know about it?
    #
    if add_worldmap_layerinfo_if_exists(tabular_info):
        # A WorldMap layer already exists!
        if not tabular_info.column_names:
            # Make sure the tabular file has been read and we have
            # data on it (e.g. may be re-mapping now and tabular info is
            # freshly retrieved, columns not yet formatted, etc)
            #
            tab_file_stats = TabFileStats.create_from_tabular_info(tabular_info)
            if tab_file_stats.has_error():
                raise Http404(tab_file_stats.error_message)

        worldmap_tabularinfo = tabular_info.get_worldmap_info()
        if worldmap_tabularinfo:
            return view_existing_map(request, worldmap_tabularinfo)


    # ----------------------------------
    # Open the file and get the stats
    # ----------------------------------
    tab_file_stats = TabFileStats.create_from_tabular_info(tabular_info)
    if tab_file_stats.has_error():
        raise Http404(tab_file_stats.error_message)

    # preview rows
    num_preview_rows = min([x for x in (NUM_PREVIEW_ROWS, tabular_info.num_rows) if x > 0])

    # ----------------------------------
    # Format values for geospatial data type dropdown
    # ----------------------------------
    # Always include lat/lng as a dropdown choice
    geocode_type_list = [(GEO_TYPE_LATITUDE_LONGITUDE, u'Latitude/Longitude')]

    # Retrieve available Geocode types and join Layers
    (geocode_types_from_worldmap, available_layers_list) = get_geocode_types_and_join_layers()

    # Add WorldMap join targets (if any)
    if geocode_types_from_worldmap:
        geocode_type_list += geocode_types_from_worldmap

    # Separate list of all join layers (which are filtered by type on form)
    #print 'available_layers_list', available_layers_list
    # ----------------------------------
    # Create a Django form for table join column selection
    # ----------------------------------
    #print 'tab_file_stats.column_names', type(tab_file_stats.column_names)

    if available_layers_list and len(available_layers_list) > 0:
        form_single_column = ChooseSingleColumnForm(\
                                    tabular_file_info_id=tabular_info.id,
                                    layer_choices=available_layers_list,
                                    column_names=tab_file_stats.column_names)
    else:
        form_single_column = None

    # ----------------------------------
    # Create a form for Lat/Lng column selection
    # ----------------------------------
    if tab_file_stats:
        form_lat_lng = LatLngColumnsForm(\
                            tabular_file_info_id=tabular_info.id,\
                            column_names=tab_file_stats.column_names)
    else:
        form_lat_lng = None

    template_dict = get_common_lookup(request)

    template_dict.update(dict(\
                    tabular_id=tabular_info.id,
                    tabular_md5=tabular_info.md5,
                    gis_data_info=tabular_info,
                    tab_file_stats=tab_file_stats,
                    geocode_types=geocode_type_list,
                    NUM_PREVIEW_ROWS=num_preview_rows,
                    test_files=TabularFileInfo.objects.all(),
                    form_single_column=form_single_column,
                    form_lat_lng=form_lat_lng,
                    GEO_TYPE_LATITUDE_LONGITUDE=GEO_TYPE_LATITUDE_LONGITUDE,
                    page_title=PANEL_TITLE_MAP_DATA_FILE))

    template_dict[GEOCONNECT_STEP_KEY] = STEP1_EXAMINE
    template_dict['GEOCONNECT_STEPS'] = GEOCONNECT_STEPS

    return render(request, 'tabular_files/main_outline_tab.html', template_dict,)
Ejemplo n.º 16
0
def view_mapit_incoming_token64(request, dataverse_token):
    """
    (1) Check incoming url for a callback key 'cb'
        and use the callback url to retrieve the DataverseInfo via a POST
    (2) Route the request depending on the type of data returned
    """
    maintenance_notice = get_maintenance_snippet()
    if maintenance_notice is not None:
        info_dict = get_common_lookup(request)
        info_dict['maintenance_notice'] = maintenance_notice
        return render(request,
                      'content_pages/view_home.html',
                      info_dict)

    # (1) Check incoming url for a callback url
    # and use the url to retrieve the DataverseInfo via a POST
    #
    request_helper = InitialRequestHelper(request, dataverse_token)
    if request_helper.has_err:
        return view_formatted_error_page(request,\
                            request_helper.err_type,\
                            request_helper.err_msg)


    # (2) Route the request depending on the type of data returned
    #
    mapping_type = request_helper.mapping_type

    #  Is the mapping type valid?
    #  Knowingly redundant, also checked in requestHelper
    #
    if not is_valid_dv_type(mapping_type):
        err_msg = 'The mapping_type for this metadata was not valid.  Found: %s' % mapping_type

        return view_formatted_error_page(request,\
                            FAILED_TO_IDENTIFY_METADATA_MAPPING_TYPE,\
                            err_msg)

    #  Is the mapping type active?
    #
    if not is_setting_active(mapping_type):
        return view_filetype_note_by_name(request, mapping_type)

    # Let's route it!
    #
    if is_dv_type_shapefile(mapping_type):

        return process_shapefile_info(request,\
                            request_helper.dataverse_token,\
                            request_helper.dv_data_dict)

    elif is_dv_type_tabular(mapping_type):

        return process_tabular_file_info(request,\
                            request_helper.dataverse_token,\
                            request_helper.dv_data_dict)

    elif is_dv_type_geotiff(mapping_type):

        err_msg = 'Sorry! GeoTiff mapping is currently not available'
        return view_formatted_error_page(None, err_msg)

    return HttpResponse('Error!!  Should never reach this line!')
Ejemplo n.º 17
0
def view_shapefile(request, shp_md5, **kwargs):
    """
    (1) Does a map of this shapefile exist in the db?

    (2) Does a map of this shapefile exist in WorldMap?

    (3) Show the initial page with "Visualize" button

    This is fantastically long and messy -- need to break it up

    Retrieve and view a :model:`gis_shapefiles.ShapefileInfo` object

    :shp_md5: unique md5 hash for a :model:`gis_shapefiles.ShapefileInfo`
    :template:`shapefiles/main_outline_shp.html`
    """
    LOGGER.debug('view_shapefile 1')
    # -------------------------------------------
    # Flags for template - Is this the first time the file is being visualized?
    # -------------------------------------------
    first_time_notify = kwargs.get('first_time_notify', False)

    # Attempt to retrieve the shapefile information
    # -------------------------------------------
    LOGGER.debug('view_shapefile 2 - Attempt to retrieve the shapefile information')
    try:
        shapefile_info = ShapefileInfo.objects.get(md5=shp_md5)
    except ShapefileInfo.DoesNotExist:
        raise Http404('Shapefile not found.')

    # -------------------------------------------------------------------
    # Does a fully checked shapefile exist with a worldmap layer?
    # -------------------------------------------------------------------
    LOGGER.debug('view_shapefile 3 - Does a shapefile exist with a worldmap layer?')

    shp_service = SendShapefileService(**dict(shapefile_info=shapefile_info))
    if shp_service.flow1_does_map_already_exist():
        LOGGER.debug('view_shapefile 3a - Map already exists')

        worldmap_layerinfo = shp_service.get_worldmap_layerinfo()
        if worldmap_layerinfo is None:
            return HttpResponse('<br />'.join(shp_service.err_msgs))
        else:
            MetadataUpdater.run_update_via_popen(worldmap_layerinfo)
            return view_classify_shapefile(request, worldmap_layerinfo, first_time_notify)

    # -------------------------------------------
    # Gather common parameters for the template
    # -------------------------------------------
    d = get_common_lookup(request)
    d['gis_data_info'] = shapefile_info
    d['shapefile_info'] = shapefile_info
    d['page_title'] = PANEL_TITLE_MAP_DATA_FILE
    d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL
    d[GEOCONNECT_STEP_KEY] = STEP1_EXAMINE
    if first_time_notify:
        d['first_time_notify'] = True

    # -------------------------------------------
    # Early pass: Validate that this .zip is a shapefile--a single shapefile
    #    - Should we move this out?  Check being done at Dataverse
    #    - Also, no need to move the file if viz already exists
    # -------------------------------------------
    LOGGER.debug('view_shapefile 4 - Validate that .zip is a single shapefile')
    if not shapefile_info.zipfile_checked:
        LOGGER.debug('zipfile_checked NOT checked')

        zip_checker = ShapefileZipCheck(shapefile_info.dv_file, **{'is_django_file_field': True})
        #zip_checker = ShapefileZipCheck(shapefile_info.get_dv_file_fullpath())
        zip_checker.validate()

        # -----------------------------
        # Error: No shapefiles found
        #   Show error message
        # -----------------------------
        if zip_checker.has_err:
            return view_zip_checker_error(request, shapefile_info, zip_checker, d)

        # -----------------------------
        # Load the single shapefile
        # -----------------------------

        shapefile_info.has_shapefile = True
        shapefile_info.zipfile_checked = True
        shapefile_info.save()

        list_of_shapefile_set_names = zip_checker.get_shapefile_setnames()
        success = zip_checker.load_shapefile_from_open_zip(list_of_shapefile_set_names[0], shapefile_info)

        if not success:
            d['Err_Found'] = True
            if zip_checker.err_type == ZIPCHECK_FAILED_TO_PROCCESS_SHAPEFILE:
                d['Err_Shapefile_Could_Not_Be_Opened'] = True
                d['zip_name_list'] = zip_checker.get_zipfile_names()
            else:
                d['Err_Msg'] = zip_checker.err_msg

            shapefile_info.has_shapefile = False
            shapefile_info.save()
            LOGGER.error('Shapefile not loaded. (%s)' % shp_md5)
            zip_checker.close_zip()
            return render(request, 'shapefiles/main_outline_shp.html', d)

    # -------------------------------------------
    # The examination failed
    # No shapefile was found in this .zip
    # -------------------------------------------
    LOGGER.debug('view_shapefile 5 - No shapefile was found in this .zip')
    if not shapefile_info.has_shapefile:
        LOGGER.debug('No shapefile found in .zip')

        d['Err_Found'] = True
        d['Err_No_Shapefiles_Found'] = True
        d['WORLDMAP_MANDATORY_IMPORT_EXTENSIONS'] = WORLDMAP_MANDATORY_IMPORT_EXTENSIONS
        return render(request, 'shapefiles/main_outline_shp.html', d)

    # -------------------------------------------------------------------
    # Shapefile fully checked, is there a worldmap layer?
    # -------------------------------------------------------------------
    LOGGER.debug('view_shapefile 6 - Shapefile fully checked, is there a worldmap layer? ')
    shp_service = SendShapefileService(**dict(shapefile_info=shapefile_info))

    if shp_service.flow1_does_map_already_exist():
        LOGGER.debug('view_shapefile 6a - Worldmap layer exists')

        worldmap_layerinfo = shp_service.get_worldmap_layerinfo()
        if worldmap_layerinfo is None:
            return HttpResponse('<br />'.join(shp_service.err_msgs))
        else:
            MetadataUpdater.run_update_via_popen(worldmap_layerinfo)
            return view_classify_shapefile(request, worldmap_layerinfo, first_time_notify)

    return render(request, 'shapefiles/main_outline_shp.html', d)
Ejemplo n.º 18
0
def view_tabular_file(request, tab_md5):
    """
    View tabular file that has been sent over via Dataverse
    """
    # ----------------------------------
    # Retrieve the Tabular file information
    # ----------------------------------
    try:
        tabular_info = TabularFileInfo.objects.get(md5=tab_md5)
    except TabularFileInfo.DoesNotExist:
        raise Http404('No TabularFileInfo for md5: %s' % tab_md5)

    # ----------------------------------
    # Does the file already have an associated layer
    # ----------------------------------
    worldmap_tabularinfo = tabular_info.get_worldmap_info()
    if worldmap_tabularinfo is not None:
        # A map exists: show it!
        return view_existing_map(request, worldmap_tabularinfo)

    #
    # Is there a WorldMap layer but Geoconnect doesn't know about it?
    #
    if add_worldmap_layerinfo_if_exists(tabular_info):
        worldmap_tabularinfo = tabular_info.get_worldmap_info()
        if worldmap_tabularinfo:
            return view_existing_map(request, worldmap_tabularinfo)

    # ----------------------------------
    # Open the file and get the stats
    # ----------------------------------
    tab_file_stats = TabFileStats.create_tab_stats_from_tabular_info(
        tabular_info)
    if tab_file_stats.has_error():
        raise Http404(tab_file_stats.error_message)

    # preview rows
    num_preview_rows = min(
        [x for x in (NUM_PREVIEW_ROWS, tabular_info.num_rows) if x > 0])

    # ----------------------------------
    # Format values for geospatial data type dropdown
    # ----------------------------------
    # Always include lat/lng as a dropdown choice
    geocode_type_list = [(GEO_TYPE_LATITUDE_LONGITUDE, u'Latitude/Longitude')]

    # Retrieve available Geocode types and join Layers
    (geocode_types_from_worldmap,
     available_layers_list) = get_geocode_types_and_join_layers()

    # Add WorldMap join targets (if any)
    if geocode_types_from_worldmap:
        geocode_type_list += geocode_types_from_worldmap

    # Separate list of all join layers (which are filtered by type on form)
    #print 'available_layers_list', available_layers_list
    # ----------------------------------
    # Create a Django form for table join column selection
    # ----------------------------------
    #print 'tab_file_stats.column_names', type(tab_file_stats.column_names)

    if available_layers_list and len(available_layers_list) > 0:
        form_single_column = ChooseSingleColumnForm(
            tabular_file_info_id=tabular_info.id,
            layer_choices=available_layers_list,
            column_names=tab_file_stats.column_names)
    else:
        form_single_column = None

    # ----------------------------------
    # Create a form for Lat/Lng column selection
    # ----------------------------------
    if tab_file_stats:
        form_lat_lng = LatLngColumnsForm(tabular_file_info_id=tabular_info.id,\
                    column_names=tab_file_stats.column_names)
    else:
        form_lat_lng = None

    template_dict = get_common_lookup(request)

    template_dict.update(
        dict(tabular_id=tabular_info.id,
             tabular_md5=tabular_info.md5,
             gis_data_info=tabular_info,
             tab_file_stats=tab_file_stats,
             geocode_types=geocode_type_list,
             NUM_PREVIEW_ROWS=num_preview_rows,
             test_files=TabularFileInfo.objects.all(),
             form_single_column=form_single_column,
             form_lat_lng=form_lat_lng,
             GEO_TYPE_LATITUDE_LONGITUDE=GEO_TYPE_LATITUDE_LONGITUDE,
             page_title=PANEL_TITLE_MAP_DATA_FILE))

    template_dict[GEOCONNECT_STEP_KEY] = STEP1_EXAMINE
    template_dict['GEOCONNECT_STEPS'] = GEOCONNECT_STEPS

    return render(
        request,
        'tabular_files/main_outline_tab.html',
        template_dict,
    )
Ejemplo n.º 19
0
def view_shapefile(request, shp_md5, **kwargs):
    """
    (1) Does a map of this shapefile exist in the db?

    (2) Does a map of this shapefile exist in WorldMap?

    (3) Show the initial page with "Visualize" button

    This is fantastically long and messy -- need to break it up

    Retrieve and view a :model:`gis_shapefiles.ShapefileInfo` object

    :shp_md5: unique md5 hash for a :model:`gis_shapefiles.ShapefileInfo`
    :template:`shapefiles/main_outline_shp.html`
    """
    LOGGER.debug('view_shapefile 1')
    # -------------------------------------------
    # Flags for template - Is this the first time the file is being visualized?
    # -------------------------------------------
    first_time_notify = kwargs.get('first_time_notify', False)

    # Attempt to retrieve the shapefile information
    # -------------------------------------------
    LOGGER.debug(
        'view_shapefile 2 - Attempt to retrieve the shapefile information')
    try:
        shapefile_info = ShapefileInfo.objects.get(md5=shp_md5)
    except ShapefileInfo.DoesNotExist:
        raise Http404('Shapefile not found.')

    # -------------------------------------------------------------------
    # Does a fully checked shapefile exist with a worldmap layer?
    # -------------------------------------------------------------------
    LOGGER.debug(
        'view_shapefile 3 - Does a shapefile exist with a worldmap layer?')

    shp_service = SendShapefileService(**dict(shapefile_info=shapefile_info))
    if shp_service.flow1_does_map_already_exist():
        LOGGER.debug('view_shapefile 3a - Map already exists')

        worldmap_layerinfo = shp_service.get_worldmap_layerinfo()
        if worldmap_layerinfo is None:
            return HttpResponse('<br />'.join(shp_service.err_msgs))
        else:
            MetadataUpdater.run_update_via_popen(worldmap_layerinfo)
            return view_classify_shapefile(request, worldmap_layerinfo,
                                           first_time_notify)

    # -------------------------------------------
    # Gather common parameters for the template
    # -------------------------------------------
    d = get_common_lookup(request)
    d['gis_data_info'] = shapefile_info
    d['shapefile_info'] = shapefile_info
    d['page_title'] = PANEL_TITLE_MAP_DATA_FILE
    d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL
    d[GEOCONNECT_STEP_KEY] = STEP1_EXAMINE
    if first_time_notify:
        d['first_time_notify'] = True

    # -------------------------------------------
    # Early pass: Validate that this .zip is a shapefile--a single shapefile
    #    - Should we move this out?  Check being done at Dataverse
    #    - Also, no need to move the file if viz already exists
    # -------------------------------------------
    LOGGER.debug('view_shapefile 4 - Validate that .zip is a single shapefile')
    if not shapefile_info.zipfile_checked:
        LOGGER.debug('zipfile_checked NOT checked')

        zip_checker = ShapefileZipCheck(shapefile_info.dv_file,
                                        **{'is_django_file_field': True})
        #zip_checker = ShapefileZipCheck(shapefile_info.get_dv_file_fullpath())
        zip_checker.validate()

        # -----------------------------
        # Error: No shapefiles found
        #   Show error message
        # -----------------------------
        if zip_checker.has_err:
            return view_zip_checker_error(request, shapefile_info, zip_checker,
                                          d)

        # -----------------------------
        # Load the single shapefile
        # -----------------------------

        shapefile_info.has_shapefile = True
        shapefile_info.zipfile_checked = True
        shapefile_info.save()

        list_of_shapefile_set_names = zip_checker.get_shapefile_setnames()
        success = zip_checker.load_shapefile_from_open_zip(
            list_of_shapefile_set_names[0], shapefile_info)

        if not success:
            d['Err_Found'] = True
            if zip_checker.err_type == ZIPCHECK_FAILED_TO_PROCCESS_SHAPEFILE:
                d['Err_Shapefile_Could_Not_Be_Opened'] = True
                d['zip_name_list'] = zip_checker.get_zipfile_names()
            else:
                d['Err_Msg'] = zip_checker.err_msg

            shapefile_info.has_shapefile = False
            shapefile_info.save()
            LOGGER.error('Shapefile not loaded. (%s)' % shp_md5)
            zip_checker.close_zip()
            return render(request, 'shapefiles/main_outline_shp.html', d)

    # -------------------------------------------
    # The examination failed
    # No shapefile was found in this .zip
    # -------------------------------------------
    LOGGER.debug('view_shapefile 5 - No shapefile was found in this .zip')
    if not shapefile_info.has_shapefile:
        LOGGER.debug('No shapefile found in .zip')

        d['Err_Found'] = True
        d['Err_No_Shapefiles_Found'] = True
        d['WORLDMAP_MANDATORY_IMPORT_EXTENSIONS'] = WORLDMAP_MANDATORY_IMPORT_EXTENSIONS
        return render(request, 'shapefiles/main_outline_shp.html', d)

    # -------------------------------------------------------------------
    # Shapefile fully checked, is there a worldmap layer?
    # -------------------------------------------------------------------
    LOGGER.debug(
        'view_shapefile 6 - Shapefile fully checked, is there a worldmap layer? '
    )
    shp_service = SendShapefileService(**dict(shapefile_info=shapefile_info))

    if shp_service.flow1_does_map_already_exist():
        LOGGER.debug('view_shapefile 6a - Worldmap layer exists')

        worldmap_layerinfo = shp_service.get_worldmap_layerinfo()
        if worldmap_layerinfo is None:
            return HttpResponse('<br />'.join(shp_service.err_msgs))
        else:
            MetadataUpdater.run_update_via_popen(worldmap_layerinfo)
            return view_classify_shapefile(request, worldmap_layerinfo,
                                           first_time_notify)

    return render(request, 'shapefiles/main_outline_shp.html', d)