Showing posts with label OCI. Show all posts
Showing posts with label OCI. Show all posts

Monday, September 23, 2024

How to Configure Spatial Geometry Objects in Geolocation Maps in Oracle APEX

In this post, we saw how to implement Geolocation Maps in Oracle Apex. In many use cases, you may need to draw certain features using coordinate and geometry information. Spatial Geometry objects can be used in such cases to make the geoprocessing easier.


Spatial geometry objects are drawn on the background map based on data in a table or returned by our query. Apex Maps support a rich set of built-in marker icons and shapes. Below are the supported spatial geometry objects in Apex Maps Region:


Points - Points display as markers e.g. user's current location or any specific point on the map.

Lines - Lines represent features like roads or it can be line drawn between variety of points.

Polygons - Polygons represent areas like states or countries or it can be drawn to show any other areas on the map.

Heat Map - Heat Maps are used to visualize the point density.

Extruded Polygons - These are displayed as three-dimensional, extruded Polygons. The height of the 3D object is based on a Numeric column value returned by our query.


Spatial geometry objects can be based on below sources:

Geometry Column - Supported datatypes include SDO_GEOMETRY, VARCHAR2, or CLOB. VARCHAR2 and CLOB columns must contain geometry information in GeoJSON format.

Numeric Columns - Columns representing longitude and latitude values but this option applies only to Point and Heat Map objects.


Let's take a took at each of these Spatial Geometry Objects and how we can use them in Apex Maps Region.



1. Points:

- Let's create a Layer in our Map

Layer Type: Points

As we learned above, Points layer can be based on Numeric values one for Latitude and other for Longitude. So let's use a simple query with sample values indicating a specific point on the map.


SQL Query:

SELECT TO_NUMBER('40.79261211884605') P_LAT 
      ,TO_NUMBER('-73.95252971760179') P_LONG
FROM DUAL




- Let's specify fa-location-arrow in Icon CSS Classes under Point Objects section.




- Run the App.


- As we can see the Point is denoted by an Arrow at the exact point based on Latitude and Longitude.






2. Lines:

- Let's create a new Layer in our Map

Layer Type: Lines

Lines layer needs GeoJSON format as the source. So let's build a query to generate data in GeoJSON format.
This will consist of any two points on the map (from and to) and their corresponding Latitude and Longitude values.


SQL Query:

SELECT 
'
{
"type": "LineString",
"coordinates": [
[
-73.95252971760179,
40.79261211884605
],
[
-73.95345327758079,
40.791114880267148
]
]
}
' geojson_val
FROM DUAL




- Let's specify the line properties:

Stroke Style: Dotted

Stroke Color: Red

Stroke Width: 7





- Run the App.


- As we can see a dotted Line is drawn over our map in Red color based on our coordinates.

We can also see a new check-box has been added on the map indicating controls for Line layer.








3. Polygons:

- Let's create a new Layer in our Map

Layer Type: Polygons

Polygons layer needs GeoJSON format as the source. So let's build a query to generate data in GeoJSON format.
This will consist of a number of points on the map (forming a Polygonal shape) and their corresponding Latitude and Longitude values. I'm forming the Polygon using 5 points on the map.


SQL Query:

SELECT 
'
{
"type": "Polygon",
"coordinates": [
[
[
-73.9684379656287,
40.78569943754278
],
[
-73.9629016692047,
40.78849384000406
],
[
-73.95766551865343,
40.789760807246765
],
[
-73.95895386999608,
40.783522052350584
]
,
[
-73.96569143411632,
40.7829049598581
]
,
[
-73.9684379656287,
40.78569943754278
]
]
]
}
' geojson_val
FROM DUAL







- Let's specify the Appearance properties:

Fill Color: Grey

Fill Opacity: 0.5

Stroke Color: Red







- Run the App.


- As we can see a Semi-Transparent Polygon drawn over our map in Red color based on our coordinates.

We can also see a new check-box has been added on the map indicating controls for Polygon layer.






4. Extruded Polygons:

- Let's create a new Layer in our Map

Layer Type: Extruded Polygons

Polygons layer needs GeoJSON format as the source. So let's build a query to generate data in GeoJSON format.
This will consist of a number of points on the map (forming a Polygonal shape) and their corresponding Latitude and Longitude values. I'm forming the Polygon using 4 points on the map to show a building in 3D format.

Here' we can see the query also has another column E_VAL with value 70 . This indicates the Extrusion Value for this layer which is basically the value to determine the height of the extruded polygon.


SQL Query:

SELECT 
'
{
"type": "Polygon",
"coordinates": [
[
[
-73.95243322590251,
40.79222980941566
],
[
-73.95211869237345,
40.79209869069768
],
[
-73.95250182333042,
40.79162315684402
],
[
-73.95284231859179,
40.791672689498604
]
,
[
-73.95243322590251,
40.79222980941566
]
]
]
}
' geojson_val
,70 E_VAL
FROM DUAL






- Let's specify the Appearance properties:

Fill Color: Lavender

Extrusion Value Column: E_VAL (This is the column returned by our query)

Unit: Meter

Fill Opacity: 0.5







- Run the App.


- As we can see a Semi-Transparent Polygon drawn over a building in Lavender color based on our coordinates.

We can also see a new check-box has been added on the map indicating controls for Extruded Polygon layer.






This is how we can leverage various Spatial Geometry Objects in Oracle Apex Map Region to draw a variety of objects over our Geolocation Maps.





Share:

Tuesday, July 23, 2024

Implementing Geolocation Maps in Oracle APEX

Geolocation Maps have become a crucial part in many applications built in Oracle APEX. Prior to release 21.1, developers had to rely on third-party geolocation map providers and had to obtain API keys and third-party plugins to integrate Geolocation Maps in Oracle Apex.

But since release 21.1, Oracle has introduced native Maps region in Apex. It eliminates the need to obtain third-party plugins, API keys and best of all - it's free.

Map component is based on Oracle Elocation Service. More details can be found at https://maps.oracle.com. 
Map region supports Vector map layers based on OpenStreetMap Position, OpenStreetMap Bright, and OpenStreetMap DarkMatter.

Let's see how to implement this functionality in our Apex App.

- Create a new App in Oracle Apex

- Create a new page

- Create a page item to store GeoLocation details




- Create a Region and set type to Map






- Set the SQL Query in source of Map region to 


SELECT :P4_GEOLOC GEOJSON_COL FROM DUAL

Set Page Items to Submit to P4_GEOLOC




- Now, let's see how we can obtain the current location of the user

- Navigate to Dynamic Actions

- Create a DA at Page Load level



- Set Action to 'Get Current Position'



- Set Return Type to GeoJSON
- Set Item to P4_LATLONG



This will enable our App to acquire device's current location and return Geolocation in GeoJSON format and store it in item P4_LATLONG


About GeoJSON:
GeoJSON is a geospatial data interchange format based on JavaScript Object Notation (JSON).  It defines several types of JSON objects and the manner in which they are combined to represent data about geographic features, their properties, and their spatial extents. Broadly it accommodates 7 different kinds of geometry objects: "Point", "LineSegment", "Polygon", "MultiPoint", "MultiLineString", "MultiPolygon", and "GeometryCollection".

In above example, it will return the data as shown in below example:

{
    "latitude":20.7195718,
    "longitude":-103.4363326,
    "altitude":null,
    "accuracy":16.452,
    "altitudeAccuracy":null,
    "heading":null,
    "speed":null
}


BUT the Map Region in Oracle Apex requires the data in below GeoLocation format:

{
"type": "point",
"coordinates": [
Longitude,
Latitude
]
}

- So we need to Transform the data returned by Get Current Position action into this format.

- Let's create another Dynamic Action named Transform with Action as Execute Server-side code and embed below PLSQL code in it. We will be using json_table function to transform the data in the desired format.

Set Items to submit as P4_LATLONG
Set Items to Return as P4_GEOLOC

P4_GEOLOC is the item we originally used in the Source of Map region



Code:

declare
   lv_geojson varchar2(4000);
begin
 
   with device_json as (
           select :P4_LATLONG json_res 
           from dual )
 
   select '{"type": "Point", "coordinates": ['||json_data.longitude||', '||json_data.latitude||']}' coordinates
       into lv_geojson 
       from device_json,
       json_table(json_res, '$[*]'
          columns (longitude varchar2(100) path '$.longitude',
                   latitude  varchar2(100) path '$.latitude')
       ) as json_data;
 
   :P4_GEOLOC := lv_geojson;
 
end;



- Let's add another Dynamic Action to refresh the map



- Let's run the App and see what we see.

- Here, we can see the Map region shows my current location and it shows I'm somewhere in Seattle area.




- Let's zoom into the map and we'll see my exact location which is near the famous Starbucks Pike Place Market location.




- In above examples, if you notice you will see it says OpenStreetMap in the footnote of the map.

This is because I've selected OpenStreetMap Bright as the Standard in Map Region Attributes




You can experiment using all these options and select the one that suits your use case the best.

Here's the output using Oracle World Map standard





Using Latitude/Longitude instead of GeoJSON:

We saw how we can obtain the geolocation data in GeoJSON format using Get Current Position action.

There's another type supposed by this action and that is Latitude and Longitude values. 
If we want to only obtain these two and do not require other values such as Altitude, Speed etc., then we can use this method. Let's take a look into that.

- Let's create two separate page items one to store Latitude and other for Longitude and make them hidden




- Now, let's navigate back to our Dynamic Action and let's change the Return Type to Latitude and Longitude






- Now, assign P4_LAT and P4_LONG page items as below.



This will return the Latitude and Longitude values of user's current location in these page items.

- Now, let's navigate to Map Region

- Set Source SQL Query as below. This will simply use the Latitude and Longitude values stored in hidden page items.

Note: We need to convert these to NUMBER format before using it in Map Region


SELECT TO_NUMBER(:P4_LAT) P_LAT 
      ,TO_NUMBER(:P4_LONG) P_LONG
FROM DUAL




- Set Page Items to Submit to P4_LAT,P4_LONG

- Navigate to Column Mapping section under Map Layer

- Set Geometry Column Data Type to Longitude/Latitude

- Set the page items corresponding values




- Now, let's assume the user is near Museum of the City of New York while accessing the App, the Map Region will precisely show his location as below -





Share:

Wednesday, June 26, 2024

Unleashing the power of Artificial Intelligence in Oracle APEX 24.1

Oracle APEX has been a game-changer in the low-code application development  domain, enabling developers to build robust applications rapidly. 

Oracle recently released Apex version 24.1 which is loaded with AI features. With the introduction of the AI Assistant in Oracle APEX in version 24.1, APEX has elevated its capabilities, offering AI support to developers. Let's see what are the features and benefits of the Oracle APEX AI Assistant and how to configure and use it in a step by step tutorial.


What is the Oracle APEX AI Assistant?

The Oracle APEX AI Assistant is an intelligent, context-aware virtual assistant designed to help developers and end-users interact more efficiently with their APEX applications. 

It leverages natural language processing (NLP) and machine learning (ML) to understand user queries and automate routine tasks.

AI Assistant has ability to understand and process natural language queries. Developers and users can interact with the assistant using plain language, asking questions and giving commands to generate queries and/or automate repetitive tasks.


Steps to configure and use the APEX AI Assistant

First we need to configure Generative AI service in our APEX environment:


- Navigate to Workspace Utilities > All Workspace Utilities



- Navigate to Generative AI option



- Create a new service


- Let's select Open AI for the AI Provider


- You can choose Cohere, Open AI or Oracle Generative AI service providers






AI Provider: Open AI

Name: OpenAI

Used by App Builder: Make sure to enable this option

Base URL (auto-populated): https://api.openai.com/v1

Credentials (Create New): Obtain the Open AI API key and paste it here

Note: You can create a free Open AI account and generate an API key under Trial Keys section





- Click Create

- Once created, it should look like this




- With these steps, we have configured the Gen-AI Service in our APEX environment.


APEX AI Assistant Features

Once the Gen-AI Service is configured in APEX environment, below features will be automatically activated in our environment:

Create App Using Generative AI:
This feature facilitates the creation of new application from a natural language user prompt that specifies desired capabilities. This simplifies the development of new applications and allows developers to focus their energy and resources on creating unique, bespoke application features rather than underlying code.

Conversational AI dialogs:
This feature enables one to 'converse' with their application and database in natural language through an out-of-the-box conversational interface that developers can easily add to their applications. As a result, developers can provide a richer in-app experience without the need to build Generative AI or natural language processing components from scratch.

Let's take a look at these features in detail.


Create App Using Generative AI

- Navigate to App Builder and click Create a New App option



- Here, we will notice a new option named 'Create App Using Generative AI'. Select it.



- Let's mention below sentence to Gen-AI Engine to create a new App to manage Projects data.

"Create an app to maintain project milestones, project tasks, projects, project task todos, and project status, that includes pages for a chart, dashboard, interactive report, and faceted search."



- As we can see the Gen-AI Engine in our APEX Environment understood our Natural Language (NLP) prompt and has offered us an option that will create an application called Project Tracker with the pages to offer Project Overview, Dashboard, Projects interactive report and a Faceted Search page.



- Let's click Create Application

- This will take us to a final review page where we can see all above pages are listed. Let's click Create Application



- Once finished, we'll have our application ready for first use



- Let's run it and see how it works.

- Once logged in, we can see our app shows all the features including the pages described in above steps.

Project Overview:



Project Dashboard:


 
Projects List (Interactive Report):



Search Tasks (Faceted Search):



- This way we can leverage Gen-AI service to automatically create an App for us based on our Natural Language instructions.


Conversational AI dialogs


SQL Assistant:

Now, lets take a look at Conversational AI dialogs feature to 'converse' with our database in natural language and build constructs using AI.

 -Let's create a new page in our application

- Select Interactive Report and click next



- Provide name to the IR Report and select Source Type as SQL Query



- Here, we'll notice a new option named APEX Assistant

- This is another AI feature that let's us converse with our database and provide our requirements in Natural Language prompts and APEX Assistant would convert the same into SQL queries

- Let's mention a statement like "create a report based on Employees data" and see if Gen-AI decodes it and builds an Interactive Report query based on Employees and related information.




- As we can see the APEX Assistant understood our Natural Language prompt and has built the query for us. Let's click Insert to use this query.





- Validate the query and we can see the APEX AI Assistant has built a valid query for us.




- Let's run our AI Generated Interactive Report and we can see the query works perfectly.





PL/SQL Assistant


- The APEX AI Assistant is available throughout the App Builder at many places and it doesn't stop at generating SQL queries but we can use it to also generate PL/SQL Code.

- Let's create a Dynamic Action for example purpose

- Change Action to Execute Server-side code

- Under PL/SQL Code box, click Code Editor icon to popup the Code Editor

- As we can see we have APEX Assistant available under PL/SQL as well.

- Let's  mention statement 'anonymous block to maintain project tasks' and see if AI can generate a anonymous block to handle Project Tasks for us.




- As we can see, Gen-AI understood our Natural Language prompt and built an anonymous block for us that helps in performing Insert, Update and Delete operations on Project Tasks related data. We can utilize this as a template and tweak it as needed.


As seen above, the Oracle APEX AI Assistant in version 24.1 is a groundbreaking addition that significantly enhances the capabilities of the APEX platform. By leveraging AI and machine learning, it transforms the way developers interact with applications and databases, making the process more intuitive, efficient, and productive.


Share: