Coordinate Conversion/Measurement Samples¶
Coordinate conversion and measurement functions are used to change map and point projections, convert coordinates (ground, layer, and pixel), add custom datums, and more. This is useful for combining and adjusting data from various different sources and/or regions.
Using Python Libraries to Display Vector Attributes¶
Note
Before running this sample, configure these variables and modules:
export_dir
: the directory for resulting files to be exported toimport numpy as np
import matplotlib.pyplot as plt
This script places raster elevation values (in meters), and (x,y) pixel coordinate values into a python list from a defined area. The (x,y) pixel cooridnates are converted to (x,y) ground coorindate values based on the current project of the map. Ground coordinate values and elevation values are placed into a numpy array for use with Matplotlib. As an example of data visualization with external packages, a heat grid of the retrived grid of values is exported to a specified directory.
# Displays information about the raster layer
info = gm.GetLayerInfo(loaded_array[0])
print("Layer %d, 0x%x:" % (0, loaded_array[0]))
print(" Description: %s" % info.mDescription)
print(" Type: %s" % info.mTypeName)
print(" Raster Data: %s" % info.mHasRasterData)
print(" Pixel Height: %s" % info.mGlobalPixelHeight)
print(" Pixel Width: %s" % info.mGlobalPixelWidth)
print(" Max Elevation: %s" % info.mMaxElevation)
print(" Min Elevation: %s" % info.mMinElevation)
# Collects elevation values and x,y pixel coordinates converted to ground
# coordinate values
# Python lists of elevation and x,y ground coordinate values
elevation_values = []
x_coord = []
y_coord = []
# i is defined as a starting y pixel coordinate
i = 3073
# i, j are definded for 10x10 pixel coordinate grid
while i < 3083:
j = 6066
while j < 6076:
# Collects elevation values as specific x, y pixel coordinates
err_GetPixelElevation, elevation = gm.GetPixelElevation(loaded_array[0], j, i)
# X,Y pixel coordinates are converted to ground coordinates
err_ConvertCoordLayerPixelToGround, coordinateX, coordinateY = (
gm.ConvertCoordLayerPixelToGround(loaded_array[0], j, i)
)
# All values are added to their respective lists
elevation_values.append(elevation)
x_coord.append(coordinateX)
y_coord.append(coordinateY)
j += 1
i += 1
# Numpy arrays are created for elevation and x, y ground coordinates
# Elevation is changed from a 1-d to 2-d array with 10 values per row
elevation = np.array(elevation_values).reshape(-1, 10)
# Duplicates are removed from x and y ground coordinate arrays
x_values = np.unique(np.array(x_coord))
y_values = np.unique(np.array(y_coord))
# Rounds ground coordinates to 3 decimal points
x = np.round(x_values, 3)
y = np.round(y_values, 3)
# Displays a simple heat grid with Matplotlib
fig, ax = plt.subplots()
# Creates image for heat grid
im = ax.imshow(elevation)
# Sets labels for x and y coordinate values
ax.set_xticks(np.arange(len(x)), labels=x, fontsize=10)
ax.set_yticks(np.arange(len(y)), labels=y, fontsize=10)
ax.set_ylabel("Y Ground Coordinate Values")
ax.set_xlabel("X Ground Coordinate Values")
# Rotate the tick labels and set their alignment
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Loop over data dimensions and create text annotations of elevation values
for i in range(len(y)):
for j in range(len(x)):
text = ax.text(
j, i, elevation[i, j], ha="center", va="center", color="w", fontsize=6
)
# Displays title for grid
ax.set_title("Change of Elevation (meters) of a Selected Area")
fig.tight_layout()
plt.savefig(export_dir + "heat_grid.jpg")
Loading a Rectified JPG Using GCPs from a CSV¶
Note
Before running this sample, configure these variables:
resource_dir
: the directory path containing sample dataCSV_file
: the name of a CSV fileJPG_file
: the name of a JPG file
A script to interpret world-coordinate/pixel-coordinate pairs parsed from a CSV file. It builds a c-style array of ground control points and uses them to rectify the JPG layer as it loads.
# Open csv source file
source = open(resource_dir + CSV_file, "r")
# Extract the numeric values from the source file
values = source.read().split(",")
values = [float(val) for val in values]
source.close()
# Build the list of ground control points
gcp_list = gm.GM_GroundControlPoint_array(int(len(values) / 4))
i = 0
while i + 3 < len(values):
# Assume that the csv file is formatted like: ...,pixelX,pixelY,longitude,latitude,...
the_gcp = gm.GM_GroundControlPoint_t(
values[i], values[i + 1], values[i + 2], values[i + 3]
)
gcp_list[int(i / 4)] = the_gcp
i += 4
# Rectify the layer while loading it, using the ground control points
num_gcps = int(i / 4)
err_LoadProjectionFromEPSGCode, projection = gm.LoadProjectionFromEPSGCode(4326)
err_LoadRectifiedLayer, layer = gm.LoadRectifiedLayer(
resource_dir + JPG_file, gm.GM_LoadFlags_UseDefaultLoadOpts, gcp_list, num_gcps, projection
)