def get(self, request, shp_md5):
        """Use the SendShapefileService to create a map from a shapefile.

        - SendShapefileService takes care of details starting with retrieving
        the ShapefileInfo object
        """

        # OK if shp_md5 is None, SendShapefileService creates error message
        #
        send_shp_service = SendShapefileService(**dict(shp_md5=shp_md5))

        # Send the shapefile to WorldMap
        #
        success = send_shp_service.send_shapefile_to_worldmap()


        # -----------------------------------
        # Did it work? NOPE!  Failed along the way!
        # -----------------------------------
        if not success:
            err_note = ('Sorry! The shapefile mapping did not work.'
                        '<br /><span class="small">{0}</span>').format(\
                            '<br />'.join(send_shp_service.err_msgs))
            LOGGER.error(err_note)

            err_note_html = render_ajax_basic_err_msg(err_note,\
                                            send_shp_service.shapefile_info)

            json_msg = MessageHelperJSON.get_json_fail_msg(err_note_html, dict(id_main_panel_content=err_note_html))

            return HttpResponse(json_msg, content_type="application/json", status=200)


        # -----------------------------------
        # Yes!  We have a new map layer
        # -----------------------------------
        worldmap_shapefile_layerinfo = send_shp_service.get_worldmap_layerinfo()
        shapefile_info = worldmap_shapefile_layerinfo.get_gis_data_info()

        assert worldmap_shapefile_layerinfo is not None,\
            "Failure in SendShapefileService!  Said success but not worldmap_layerinfo (WorldMapShapefileLayerInfo)"

        # -----------------------------------------
        # Build the Map HTML to replace the form
        # -----------------------------------------
        map_html, user_message_html = build_map_html(request, worldmap_shapefile_layerinfo)
        if map_html is None:    # Failed!  Send an error
            LOGGER.error("Failed to create map HTML using WorldMapShapefileLayerInfo: %s (%d)",\
                worldmap_shapefile_layerinfo, worldmap_shapefile_layerinfo.id)

            user_msg = 'Sorry! Failed to create map. Please try again. (code: s3)'
            json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
            return HttpResponse(json_msg, content_type="application/json", status=200)

        # -----------------------------------------
        # Looks good.  In the JSON response, send
        #   back the map HTML
        # -----------------------------------------
        data_dict = dict(\
                    map_html=map_html,
                    user_message_html=user_message_html,
                    id_main_panel_title=PANEL_TITLE_STYLE_MAP,
                    message='Success! The shapefile was successfully mapped!')

        json_msg = MessageHelperJSON.get_json_success_msg("great job", data_dict=data_dict)

        return HttpResponse(json_msg, content_type="application/json", status=200)
Exemple #2
0
def view_process_lat_lng_form(request):
    """
    Create a WorldMap layer from your tabular file
    using the latitude and longitude columns selected in this form
    """

    tabular_file_info_id = request.POST.get('tabular_file_info_id', -1)

    try:
        tabular_info = TabularFileInfo.objects.get(pk=tabular_file_info_id)
    except TabularFileInfo.DoesNotExist:
        err_msg = 'Sorry! The Tabular File was not found. (tabular_file_info_id)'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)
        #raise Http404('No TabularFileInfo for id: %s' % tabular_file_info_id)

    form_lat_lng = LatLngColumnsForm(tabular_info.id,\
                        tabular_info.column_names,\
                        request.POST)
    if not form_lat_lng.is_valid():
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                    format_errors_as_text(form_lat_lng,\
                                        for_web=True)\
                                    )
        #json_msg = MessageHelperJSON.get_json_fail_msg(f.err_msg_for_web)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)


    (success, worldmap_data_or_err_msg) = create_map_from_datatable_lat_lng(\
                        tabular_info,\
                        form_lat_lng.get_latitude_colname(),\
                        form_lat_lng.get_longitude_colname(),\
                        )

    # -----------------------------------------
    # Failed! Send error message
    # -----------------------------------------
    if not success:
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                'Sorry! ' + worldmap_data_or_err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Succeeded!  Create a WorldMapTabularLayerInfo object
    # -----------------------------------------
    user_msg, response_data = worldmap_data_or_err_msg
    #json_msg = MessageHelperJSON.get_json_success_msg(user_msg, data_dict=response_data)
    #return HttpResponse(json_msg, content_type="application/json", status=200)

    worldmap_latlng_info = WorldMapTabularLayerInfo.build_from_worldmap_json(tabular_info,\
                            response_data)

    if worldmap_latlng_info is None:
        LOGGER.error("Failed to create WorldMapLatLngInfo using data: %s",\
                    response_data)
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s4)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Notify Dataverse of the new map
    # -----------------------------------------
    MetadataUpdater.update_dataverse_with_metadata(worldmap_latlng_info)

    # -----------------------------------------
    # Possible that this failed?
    # Make sure at least 1 row mapped
    # -----------------------------------------
    # Skip for now!  Error in row counts for Lat/Lng!
    """
    if worldmap_latlng_info.did_any_rows_map() is False:
        # Delete the worldmap_latlng_info object
        worldmap_latlng_info.delete()

        # Send back a user error message
        user_msg = "Sorry!  We couldn't map any of those latitude and longitude values."
        return HttpResponse(MessageHelperJSON.get_json_fail_msg(user_msg),\
                content_type="application/json",\
                status=200)
    """
    # -----------------------------------------
    # Build the Map HTML chunk to replace the form
    # -----------------------------------------
    map_html, user_message_html = build_map_html(request, worldmap_latlng_info)
    if map_html is None:  # Failed!  Send an error
        LOGGER.error("Failed to create map HTML using WorldMapLatLngInfo: %s (%d)",\
            worldmap_latlng_info, worldmap_latlng_info.id)
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s5)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Looks good.  In the JSON response, send
    #   back the map HTML
    # -----------------------------------------
    data_dict = dict(map_html=map_html,
                     user_message_html=user_message_html,
                     id_main_panel_title=PANEL_TITLE_STYLE_MAP)

    json_msg = MessageHelperJSON.get_json_success_msg("great job",
                                                      data_dict=data_dict)

    return HttpResponse(json_msg, content_type="application/json", status=200)
Exemple #3
0
def view_map_tabular_file_form(request):
    """
    AJAX call: Join your tabular file to an existing WorldMap layer
    using the column selected in this form
    """
    #for k, v in request.POST.items():
    #        print k, v

    # -----------------------------------------
    # Retrieve the id of the Tabular info object
    # -----------------------------------------
    tabular_file_info_id = request.POST.get('tabular_file_info_id', -1)

    try:
        tabular_info = TabularFileInfo.objects.get(pk=tabular_file_info_id)
    except TabularFileInfo.DoesNotExist:
        err_msg = 'Sorry! The Tabular File was not found. (tabular_file_info_id)'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)
        #raise Http404('No TabularFileInfo for id: %s' % tabular_file_info_id)

    # -----------------------------------------
    # Retrieve available Geocode types and join Layers
    #   note: geocode_types_from_worldmap not needed here
    # -----------------------------------------
    (geocode_types_from_worldmap,
     available_layers_list) = get_geocode_types_and_join_layers()

    # -----------------------------------------
    # Create form with initial + POST data
    # -----------------------------------------
    form_single_column = ChooseSingleColumnForm(tabular_info.id,\
                    available_layers_list,\
                    tabular_info.column_names,\
                    request.POST)

    # -----------------------------------------
    # Check the form's validity
    # -----------------------------------------
    if not form_single_column.is_valid():
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                        format_errors_as_text(form_single_column, for_web=True))
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    print 'cleaned_data', form_single_column.cleaned_data

    # -----------------------------------------
    # Get Dataverse info dict
    # -----------------------------------------
    dataverse_metadata_dict = get_dataverse_info_dict(tabular_info)

    # -----------------------------------------
    # Use the WorldMap API and
    # try to create a layer
    # -----------------------------------------
    tj_map_maker = TableJoinMapMaker(
        tabular_info,
        dataverse_metadata_dict,
        form_single_column.cleaned_data.get('chosen_column'),
        form_single_column.cleaned_data.get('chosen_layer'),
    )
    success = tj_map_maker.run_map_create()
    msg('success: %s' % success)
    if not success:
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                    'Sorry! ' + tj_map_maker.get_error_msg())
        msg('error msg: %s' % json_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Succeeded!  Create a WorldMapTabularLayerInfo object
    # -----------------------------------------
    worldmap_tabular_info = WorldMapTabularLayerInfo.build_from_worldmap_json(tabular_info,\
                                tj_map_maker.get_map_info())

    if worldmap_tabular_info is None:
        LOGGER.error("Failed to create WorldMapTabularLayerInfo using %s",\
            tj_map_maker.get_map_info())
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s1)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Notify Dataverse of the new map
    # -----------------------------------------
    MetadataUpdater.update_dataverse_with_metadata(worldmap_tabular_info)

    # -----------------------------------------
    # Build the Map HTML chunk to replace the form
    # -----------------------------------------
    map_html, user_message_html = build_map_html(request,
                                                 worldmap_tabular_info)
    if map_html is None:  # Failed!  Send an error
        LOGGER.error("Failed to create map HTML using WorldMapTabularLayerInfo: %s (%d)",\
            worldmap_tabular_info, worldmap_tabular_info.id)
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s2)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Looks good.  In the JSON response, send
    #   back the map HTML
    # -----------------------------------------
    data_dict = dict(map_html=map_html,
                     user_message_html=user_message_html,
                     id_main_panel_title=PANEL_TITLE_STYLE_MAP)

    json_msg = MessageHelperJSON.get_json_success_msg("great job",
                                                      data_dict=data_dict)

    return HttpResponse(json_msg, content_type="application/json", status=200)
    def get(self, request, shp_md5):
        """Use the SendShapefileService to create a map from a shapefile.

        - SendShapefileService takes care of details starting with retrieving
        the ShapefileInfo object
        """

        # OK if shp_md5 is None, SendShapefileService creates error message
        #
        send_shp_service = SendShapefileService(**dict(shp_md5=shp_md5))

        # Send the shapefile to WorldMap
        #
        success = send_shp_service.send_shapefile_to_worldmap()

        # -----------------------------------
        # Did it work? NOPE!  Failed along the way!
        # -----------------------------------
        if not success:
            err_note = "Sorry! The shapefile mapping did not work.<br /><span class='small'>%s</span>" % '<br />'.join(send_shp_service.err_msgs)
            LOGGER.error(err_note)
            err_note_html = render_ajax_basic_err_msg(err_note,\
                                            send_shp_service.shapefile_info)

            json_msg = MessageHelperJSON.get_json_fail_msg(err_note_html, dict(id_main_panel_content=err_note_html))

            return HttpResponse(json_msg, content_type="application/json", status=200)


        # -----------------------------------
        # Yes!  We have a new map layer
        # -----------------------------------
        worldmap_shapefile_layerinfo = send_shp_service.get_worldmap_layerinfo()
        shapefile_info = worldmap_shapefile_layerinfo.get_gis_data_info()

        assert worldmap_shapefile_layerinfo is not None,\
            "Failure in SendShapefileService!  Said success but not worldmap_layerinfo (WorldMapShapefileLayerInfo)"

        # -----------------------------------------
        # Build the Map HTML to replace the form
        # -----------------------------------------
        map_html, user_message_html = build_map_html(request, worldmap_shapefile_layerinfo)
        if map_html is None:    # Failed!  Send an error
            LOGGER.error("Failed to create map HTML using WorldMapShapefileLayerInfo: %s (%d)",\
                worldmap_shapefile_layerinfo, worldmap_shapefile_layerinfo.id)

            user_msg = 'Sorry! Failed to create map. Please try again. (code: s3)'
            json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
            return HttpResponse(json_msg, content_type="application/json", status=200)

        # -----------------------------------------
        # Looks good.  In the JSON response, send
        #   back the map HTML
        # -----------------------------------------
        data_dict = dict(map_html=map_html,
                    user_message_html=user_message_html,
                    id_main_panel_title=PANEL_TITLE_STYLE_MAP,
                    message='Success!  The shapefile was successfully mapped!')

        json_msg = MessageHelperJSON.get_json_success_msg("great job", data_dict=data_dict)

        return HttpResponse(json_msg, content_type="application/json", status=200)