25
Thu, Apr
0 New Articles

Tech Tip: Getting Started with Geospatial Data

Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Using geospatial analytics gives you the ability to ask and answer the “where” questions about your business.

When was the last time you searched for the nearest coffee shop, tracked the distance traveled during a car trip, or looked up the new address of a store that moved its location? In order to answer these questions, you used geospatial data. Geospatial data is data that represents an object that exists or an event that happens on the surface of the earth. An example of geospatial data is the longitude and latitude location of all coffee shops in a town. This geospatial data on its own, however, does not answer my first question: “Where is the nearest coffee shop?” Instead, I need to compare my current location to the location of all the coffee shops in my town and calculate and compare the distance between myself and each shop. From this calculation, I can find that the nearest coffee shop to my current location is less than half a mile. This combination of traditional data with the comparison and calculation of geospatial data makes up geospatial analytics.

Businesses and other agencies have many different uses for geospatial analytics. For example, they may ask questions like these:

  • Where are their stores located, where are their customers located, and what are the relationships between the two?
  • What are the historical weather patterns and trends within a 30-mile radius of a potential site of a new warehouse location?
  • What are the current locations of their key assets, such as trucks, ships, and containers? Where are these assets traveling to?

The answers to these questions can be found through the use of geospatial data and geospatial analytics. Geospatial Analytics in Db2 for i provides the tools to generate and store geospatial data in the database using user-defined geospatial distinct types combined with the ability to analyze and compare geospatial data using geospatial scalar and table functions. These new functions use integrated IBM Watson technology to perform the geospatial calculations. The IBM Watson geospatial solution uses an ellipsoidal representation of the earth that describes a round, continuous, closed surface with no edges or seams. This allows for projection-free computations, leading to more accurate and precise results.

In order to store geospatial data in a table, new user-defined geospatial distinct types exist in QSYS2. These distinct types represent the following geometries:

  • Point (QSYS2.ST_POINT)
  • Linestring (QSYS2.ST_LINESTRING)
  • Polygon (QSYS2.ST_POLYGON)
  • Geometry collection (QSYS2.ST_GEOMCOLLECTION)
  • Polygon collection (QSYS2.ST_MULTIPOLYGON)
  • Linestring collection (QSYS2.ST_MULITLINESTRING)
  • A point collection (QSYS2.ST_MULTIPOINT)

A generic geometry type (QSYS2.ST_GEOMETRY) exists that any type of geometry can be stored in.

Let’s look at an example. We’ll use the example from the very beginning of this tip and find out which coffee shop is closest to me.

First, we will create a table that can store information, including the location of coffee shops. A coffee shop is a single point, so a ST_POINT column will be used to store the geospatial data that represents the location of the coffee shop.

CREATE TABLE COFFEE_SHOPS(shop_id   CHAR(5),
                         shop_name CHAR(50),
                         address   CHAR(50),
                         city     CHAR(20),
                         state     CHAR(2),
                         zip_code CHAR(5),
                         coffee_shop_location QSYS2.ST_POINT);

In order to generate the geospatial data that will be inserted into the geospatial column, scalar functions called “constructor functions” can be used. For example, to insert data into a ST_POINT column, we will use the QSYS2.ST_POINT constructor function. This function takes a Well Known Text (WKT) representation of the longitude and latitude and converts it into binary data that can be inserted into the ST_POINT column. WKT is a text format used to represent a geometry as defined by the Open Geospatial Consortium (OGC).

INSERT INTO COFFEE_SHOPS VALUES
('00001', 'Cafe Steam', '315 S. Broadway', 'Rochester', 'MN', '55904', QSYS2.ST_POINT('point( -92.4632 44.01989)')),
('00002', 'Caribou Coffee', '3900 Marketplace Drive NW', 'Rochester', 'MN', '55901', QSYS2.ST_POINT('point(-92.50088 44.06375)')),
('00003', 'Dunn Brothers', '120 Elton Hills Drive NW', 'Rochester', 'MN', '55901', QSYS2.ST_POINT('point(-92.46361 44.04162)'));

Now that we have generated geospatial data, we can use the new geospatial scalar functions to find an answer to our question. There are 50 new geospatial functions, including both scalar functions and table functions. They can be used to answer all kinds of questions, such as whether two geometries overlap, whether two geometries intersect, what the union of two is, and much more. All of these new functions exist in QSYS2.

For our example, the question that we are answering is about distance. Therefore, we will use the ST_DISTANCE function to determine the distance between my current location and the coffee shops in the COFFEE_SHOPS table.

First, we will create a variable and store our current location in the variable. The variable is a point and therefore uses the ST_POINT datatype. Just like with the table, we will use the ST_POINT constructor function to set the variable. Then we will query our COFFEE_SHOPS table. The query uses the QSYS2.ST_DISTANCE scalar function, passing in the geospatial representation of my current location stored in the MY_LOCATION variable and the geospatial representation of the coffee shop location stored in the coffee_shop_location ST_POINT column. The ST_DISTANCE scalar function returns the distance between the two points in meters. To convert to miles, we can divide by 1609.34 to get an approximate value. In order to find the closest coffee shop, we will order the results by distance in ascending order.

CREATE VARIABLE MY_LOCATION QSYS2.ST_POINT;

SET MY_LOCATION = QSYS2.ST_POINT('point(-92.50365 44.05841)');

SELECT
SHOP_NAME,
ADDRESS,
ROUND((QSYS2.ST_DISTANCE(MY_LOCATION, coffee_shop_location)/1609.34),4)
   AS DISTANCE_MILES,
QSYS2.ST_ASTEXT(coffee_shop_location) AS LOCATION
FROM COFFEE_SHOPS
ORDER BY DISTANCE_MILES ASC;

These are the query results:

SHOP_NAME

ADDRESS

DISTANCE_MILES

LOCATION

Caribou Coffee

3900 Marketplace Drive NW

0.3942

POINT (-92.50088 44.06375)

Dunn Brothers

120 Elton Hills Drive NW

2.3046

POINT (-92.46361 44.041619999999995)

Cafe Steam

315 S. Broadway

3.3384

POINT (-92.4632 44.01989)

 

We can see that the closest coffee shop, at .3942 miles, is Caribou Coffee on Marketplace Drive. One thing to note is the use of the QSYS2.ST_ASTEXT function when returning the location column. This converts the binary geospatial data stored in the ST_POINT column back into a readable format.

Geospatial data represents the world around us. It can be used to represent the homes, business, events, mobile devices, roads, parks, states, districts, and vehicles around us. Using Geospatial Analytics gives you the ability to ask and answer the “where” questions about your business.

A new book, Geospatial Analytics, provides additional information about the new geospatial support. This new support will be provided in both IBM i 7.4 and IBM i 7.5 as of December 2022.

 

Sarah Mackenzie is a Db2 for i Senior Software Engineer. She has worked on the IBM i development team since she joined IBM in 2012. During that time, she has focused on both database and query and has worked on the design, development, and support of new enhancements for IBM i such as Temporal Tables, Db2 Mirror, and Geospatial Analytics with Watson. Sarah is also a speaker at many industry events.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: