Business Intelligence Tools / Data Science & Analytics / Developers Corner

Spatial Objects Using TERR

A key part of analytics in the oil and gas industry is evaluating opportunities at different locations. Space is always present when looking for profitable development projects. We usually look at the already in production wells and try to find some spatial trends. To stay competitive, we need to find better ways  to access the  data of different areas and its wells. For instance, we can transform the spatial information to compact objects that store the location and shape of each well and lease. These objects can be feed  to different calculations and analyses as geometries. For Spotfire, it also has some advantages, you can use the feature layers of the map chart. In this case, we can visualize the leases as polygons and wells as lines.

For these reasons, I want to cover today the topic of how to create spatial objects using TERR. Let’s start…

All the examples require the use of the sp and wkb packages.

There are several ways of creating these spatial objects. We tried to implement the most intuitive cases by looping and introducing the objects from scratch. The sp package is very rich and contains a large amount of functions to create the spatial objects. Here we use the following functions:

  1. Coordinates: this function creates a spatial point and returns a SpatialPointsDataFrame or SpatialPoints.
  2. Lines: this function package a list of objects of class “line”.
  3. SpatialLines: creates a list or data frame of objects of class “Lines”, this new object are called SpatialLines or SpatialLinesDataFrame.
  4. GridTopology: this is the function we use to generate the rectangular grid. You can use other functions to generate grids (SpatialGrid, SpatialPixels, points2grid). Make sure to study what inputs are required and the format they should have.

The wkb package allows for communication between Spotfire and TERR. The readWKB and writeWKB give the functionally to read and write the spatial objects as binary data types, which is key for introducing and exporting from the TERR data function.

Spatial Points

#keep the longitude and latitude and make sure they have no missing values
#Use the coorditate function to create the spatial points
#find the crs of your points and attach them to the object"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")

Spatial Lines

#To create lines we need the Surface and Bottom coordinates.
#create a list to store the lines
mylist= vector("list", length(Data[,1]))
# loop thru the top and bottom coordinates creating the line objects and store them as a list of lines
for (i in 1:length(Data[,1])){
#create a spatial lines object with the list of lines
#put together a data frame for output purposes. Here, we use the wkb package to save the lines as binary objects that can be exported to spotfire
LineData=data.frame(Line_Geometry=writeWKB(Lines, endian = "little"))

Spatial Polygons

#create a sequence of points to build the grid around them
difx= max(x1)- min(x1)
for (t in 1:40){
for (s in 1:40){
x1p[t*40-40+s] = min(x1)+difx*t/40
y1p[t*40-40+s] = min(y1)+dify*s/40
#define a list to save the grid polygons
mylist= vector("list", length(x1p[,1]))
#loop thru the sequence of points to create the polygon at each point. Decide the cell center, cell size and cell dimension
for (i in 1:length(mylist)){
grd = GridTopology(c(x1p[i,],y1p[i,]), c(0.1,0.1), c(1,1))
mylist[[i]]= as(grd, "SpatialPolygons")
spatgrid[i,2:7]= writeEnvelope(mylist[[i]], centerfun = mean)
spatgrid[i,1]=data.frame(writeWKB(mylist[[i]], endian = "little"))


The outputs of the three examples are ready to export from TERR as  data frames. The column with the Geometry Objects will have a binary data type and its column properties are:

  1. Content Type: application/x-wkb
  2. ColumnTypeId: Geometry

Check the following links for some template examples:



1 thought on “Spatial Objects Using TERR

  1. Jeff Reply

    Thank you so much for the post. I was able to import a csv and post each row’s coordinates as a point on a featured layer in a mapchart. However, I’m haven’t been able to post labels to said points or “edit label positions.” Any idea why or or how to get around this problem?

Leave a Reply

Your email address will not be published. Required fields are marked *