We had been working with a pre-made Global Mapper Package file called HawleyData.gmp. It’s great to have a dataset ready to go, but what happens when you are dealing with raw data? In this article, we’ll review how to extract data from a CSV file and use a similar process to recreate the geospatial product that we saw in the last article.
CSV To Python List
Comma-separated value files, also known as CSV files, are another common way to store data points. CSVs are useful for moving around information originating in a tabular format and the files can also be easily read and interpreted by humans. To demonstrate this, we will look at the CSV and regular expression (RE) libraries in order to extract the data.
Unlike JSON data, this CSV data is stored as a string (technically the data is formatted as a WKT or “Well Known Text” and is found in the ISO/IEC 13249-3:2016 specification), meaning we have to include extra lines of code to strip out the unnecessary values and format the actual data line by line. We can accomplish this by importing the (RE) library into our project.
The output you are seeing in figure X.Y is generated by printing out the row variable. Once the data is formatted, we can typecast values to strings of latitude and longitude coordinates. Inside Global Mapper’s C++ libraries, GM_Point values store latitude and longitude coordinates. These are stored as floating point values, so now we can use Python’s type casting system to convert our previously unformatted CSV into a fully functional GM_Point value!
Creating Area Features from Scratch
Using the generated coordinates, we can create an area feature using Global Mapper Pro’s Python scripting functionality. All you need is a layer to add the area feature to, a list of points, and a style to draw in.
Start by creating a new layer for our points with the function CreateCustomVectorLayer. After we create the layer, we will need to programmatically add all of our data with a for loop. Since the Python code has wrapped C++ classes, we can unpack and reference classes in order to send in the proper information and types. For this case, we will be using the gm.GM_AreaFeature_t() class to set values for each area feature. Inside each AreaFeature, we will have to create a GM_Point_t_array(), which will also need a GM_Point_t() data type in order to successfully create the area feature.
To summarize what we’ve done:
We set up a for loop that loops through our list of point features
Add an additional for loop to go through each x, y point in the list
Assign values to x and y and create a GM_Point_t class.
For this CSV data, there is no closing point value, so we need to add the first point after the for loop to close the geometry.
Access the GM_AreaFeature_t() class variables mPointList and mNumPoints and assign them the values of the classes we created.
Use the AddAreaToVectorLayer function to display the area feature.
Coloring Area Features by Value
We were able to create area features from our CSV data, but they don’t have values or colors that help visualize data in a city landscape. For that, we can utilize the JSON data provided in the project download and assign values to the area features we just created. To do this, use the function SetFeatureAttrList(), which takes in a layer, an area feature, the index of the feature, an attribute list, and the number of attributes in the list.
To color the area features, we are going to use the SetAreaFeatureDrawStyle() function and a custom color gradient function that will color the areas based on how far away from a set price they are.
Both of these functions take in additional classes as parameters similar to how the CreateCustomVectorLayer() function operates. Let’s look into the for loop and see how these additional classes help us color and assign values to these features.
To detail the prior steps, we’ve:
Create a for loop to loop over every area feature.
Created a GM_AttrValue_t() class that acts as a variable for assigning and storing data points.
Set our values for the name and value of the attributes.
Created a custom hex color based on the value of the area feature.
Created a GM_AreaStyle_t() class that holds the value of the color and the color fill option (GM_BRUSH_SOLID).
Use both the SetAreaFeatureDrawStyle() and the SetFeatureAttrList() with previously created layers and newly set parameters.
After everything, our final result will look something like this.
We’ve performed a workflow that transforms raw data in a tabular format to a polygon vector file with attribution and styling. This workflow and the concepts from this article can be applied to any kind of geospatial data. The sky is the limit when it comes to processing and creating important visualizations and depictions of geospatial data.
Companies using Blue Marble’s geospatial technology