Get an image’s alt text in a shortcode using the image URL

Instead of using the locationimage attribute, why not use the attachment ID?

This will allow you to dynamically grab the URL to the file and will make getting other meta data such as the alt text much easier. Below is a rewritten version of hplocationsfn().

Usage example: [hpLocationSquare id='2299' link='http://example.com']

Note that the link attribute is optional.

function hplocationsfn( $atts ) {
    // Set up shortcode attributes.
    // User provided values are stored in $atts.
    // Default values are passed to shortcode_atts below.
    // Merged values are stored in the $a array.
    $a = shortcode_atts( [
                'id'   => false,
                'size' => 'thumbnail',
                'link' => false,
    ], $atts );

    // Bail if we don't have an image.
    if ( ! $a['id'] ) {
        return;
    }

    // Get the image and the image's alt value.
    $image     = wp_get_attachment_image_src( $a['id'], $a['size'] );
    $image_alt = get_post_meta( $a['id'], '_wp_attachment_image_alt', true );

    // Bail if our image is invalid.
    if ( ! $image || empty ( $image ) ) {
        return;
    }

    // Generate the output.
    $output="<div class="locationSquare">";
    if ( $a['link'] ) {
        $output .= '<a href="' . esc_attr( $a['link'] ) .'">';
    }

    $output .= '<img src="' . esc_attr( $image[0] ) . '" alt="' . esc_attr( $image_alt ) . '"/>';

    if ( $a['link'] ) {
        $output .= '</a>';
    }
    $output .= '</div><!-- locationSquare -->';

    return $output;
}
add_shortcode( 'hpLocationSquare', 'hplocationsfn' );

It can be a bit of a pain for users to find the attachment ID, so here is a function that will add the attachment ID to the media overlay screen:

/**
 * Add custom field to media
 */
add_filter( 'attachment_fields_to_edit', 'wpse_attachment_fields_id', 10, 2 );
function wpse_attachment_fields_id( $fields, $post ) {

    $fields['attachment_id'] = [
            'label' =>  __( 'Attachment ID', 'text-domain' ),
            'input' => 'html',
            'value' => $post->ID,
            'html'  => "<input type="text" class="text attachment_idfield" readonly='readonly' name="attachments[{$post->ID}][attachment_id]" value="" . esc_attr( $post->ID ) . "" /><br />",
            'show_in_edit' => true,
    ];

    return $fields;         
}

Edit: Getting the attachment ID from the image URL

Alternatively, we can get the attachment ID from the image’s URL using attachment_url_to_postid().

Example usage:

[hpLocationSquare2 locationimage="http://domain.com/wp-content/uploads/2017/02/my-image.jpg" link='http://example.com/2/']

function hplocationsfn2( $atts ) {
    // Set up shortcode attrubutes.
    // User provided values are stored in $atts.
    // Default valued are passed to shortcode_atts below.
    // Merged valued are stored in the $a array.
    $a = shortcode_atts( [
                'locationimage' => false,
                'link'          => false,
    ], $atts );

    // Bail if we don't have an image.
    if ( ! $a['locationimage'] ) {
        return;
    }

    $image_id = attachment_url_to_postid( $a['locationimage'] );
    if ( ! $image_id ) {
        return;
    }

    // Get the image and the image's alt value.
    $image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );

    // Generate the output.
    $output="<div class="locationSquare">";
    if ( $a['link'] ) {
        $output .= '<a href="' . esc_attr( $a['link'] ) .'">';
    }

    $output .= '<img src="' . esc_attr( $a['locationimage'] ) . '" alt="' . esc_attr( $image_alt ) . '"/>';

    if ( $a['link'] ) {
        $output .= '</a>';
    }
    $output .= '</div><!-- locationSquare -->';

    return $output;
}
add_shortcode( 'hpLocationSquare2', 'hplocationsfn2' );