Skip to content

Commit cf81bbf

Browse files
Added sample scripts (from AskTOM session on Oct 20, 2020) under spatial directory (oracle-samples#130)
* Added examples for spatial features * Removed "how to contribute" section Since it is described at the root directory * Added "spatial" entry to the table * Added sample scripts (from AskTOM session) under /spatial * changed the name of directory
1 parent 8060f8b commit cf81bbf

File tree

3 files changed

+386
-0
lines changed

3 files changed

+386
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
--
2+
-- Stage 1 GC and Reverse GC
3+
--
4+
5+
-- Convert location information into network representation
6+
7+
-- geocoding
8+
SELECT SDO_GCDR.GEOCODE('here_sf', SDO_KEYWORDARRAY('500 oracle pky','redwood city, CA'), 'US', 'RELAX_BASE_NAME') addr FROM DUAL;
9+
10+
with
11+
part1 as (
12+
SELECT SDO_GCDR.GEOCODE('here_sf', SDO_KEYWORDARRAY('500 oracle pky','redwood city, CA'), 'US', 'RELAX_BASE_NAME') addr FROM DUAL
13+
)
14+
select a.addr.edgeid, a.addr.percent, a.addr.side, a.addr.longitude, a.addr.latitude from part1 a;
15+
16+
17+
-- reverse geocode with longitude,latitude
18+
with
19+
part1 as (
20+
SELECT SDO_GCDR.REVERSE_GEOCODE('here_sf', SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE(-122.26109, 37.5313, NULL), NULL, NULL),'US') addr FROM DUAL
21+
)
22+
select a.addr.edgeid, a.addr.percent, a.addr.side, a.addr.longitude, a.addr.latitude from part1 a;
23+
24+
25+
-- examples
26+
-- get all geocoded result of a table of streetaddress of buffer centers
27+
select * from sf_stores_streetaddress order by store_id;
28+
29+
with
30+
part1 as (
31+
SELECT b.STORE_ID, SDO_GCDR.GEOCODE('here_sf', SDO_KEYWORDARRAY(b.STREETADDRESS), 'US', 'RELAX_BASE_NAME') addr
32+
FROM SF_STORES_STREETADDRESS b
33+
)
34+
select a.store_id, a.addr.edgeid, a.addr.percent, a.addr.side, a.addr.longitude, a.addr.latitude
35+
from part1 a
36+
order by a.store_id;
37+
38+
-- transfer edgeid, percentage, and side to link_id
39+
with
40+
part1 as (
41+
SELECT b.STORE_ID, SDO_GCDR.GEOCODE('here_sf', SDO_KEYWORDARRAY(b.STREETADDRESS), 'US', 'RELAX_BASE_NAME') addr
42+
FROM SF_STORES_STREETADDRESS b
43+
)
44+
SELECT a.store_id, a.addr.longitude longitude, a.addr.latitude latitude, a.addr.side side,
45+
case
46+
when a.addr.side = 'L' then -a.addr.edgeid
47+
else a.addr.edgeid
48+
end as link_id,
49+
case
50+
when a.addr.side = 'L' then 1-a.addr.percent
51+
else a.addr.percent
52+
end as percent
53+
from part1 a
54+
order by a.store_id;
55+
56+
57+
--
58+
-- Stage 2 , Create Network Buffers
59+
--
60+
61+
-- Generate network buffer tables network buffer prefix: ('SF') in NDM java api
62+
63+
--
64+
-- now create network buffers with NDM Java Code
65+
--
66+
67+
-- 16g RAM linux box
68+
-- ~1s per buffer including analysis and persistence for 10-minute-drive.
69+
70+
-- stoer buffers
71+
72+
-- describe network buffer tables
73+
74+
-- metadata table
75+
-- buffer id, radius and direction
76+
select * from sf_nbr$ order by buffer_id;
77+
78+
-- metadata table
79+
-- buffer network representation
80+
select * from SF_NBCL$ order by buffer_id;
81+
82+
83+
-- coverage table:
84+
-- network buffer covered link table schema
85+
select * from SF_NBL$ where rownum < 20;
86+
87+
-- number of covered links of each buffer (10-minute-drive)
88+
select buffer_id, count(*) from SF_nbl$ group by buffer_id order by buffer_id;
89+
90+
91+
-- 10 min. path buffer used in the second demo:
92+
93+
-- metadata
94+
-- buffer_id, radius, direction
95+
select * from path_10m_nbr$ order by buffer_id;
96+
97+
-- buffer centers for each buffer
98+
select buffer_id,count(*) from PATH_10M_NBcL$ group by buffer_id order by buffer_id;
99+
100+
-- no of covered links for each 10-minute buffer
101+
select buffer_id, count(*) from PATH_10M_NBL$ group by buffer_id order by buffer_id;
102+
103+
104+
105+
106+
--
107+
-- Stage 3, Query Network Buffers
108+
--
109+
110+
-- queries on network buffer table
111+
112+
-- get cost to one specific buffer centers of a given location (link_id, percentage)
113+
SELECT buffer_id, MIN(start_cost+(0.95-start_percentage)*(end_cost-start_cost)/(end_percentage-start_percentage)) cost
114+
FROM SF_NBl$
115+
WHERE link_id=945669955
116+
AND buffer_id = 1
117+
AND (0.95-start_percentage)*(end_percentage-start_percentage)>=0
118+
GROUP BY buffer_id;
119+
120+
-- get costs to all buffer centers of a given location (link_id, percentage)
121+
SELECT buffer_id, MIN(start_cost+(0.95-start_percentage)*(end_cost-start_cost)/(end_percentage-start_percentage)) cost
122+
FROM SF_NBl$
123+
WHERE link_id=945669955
124+
AND (0.95-start_percentage)*(end_percentage-start_percentage)>=0
125+
GROUP BY buffer_id
126+
ORDER BY COST;
127+
128+
-- get links of the shortest path from a given location to a specific buffer center
129+
select buffer_id, link_id, prev_link_id from sf_nbl$
130+
where buffer_id = 1
131+
start with link_id = 799415310
132+
connect by prior prev_link_id = link_id and
133+
prior start_cost = end_cost and
134+
prior buffer_id = buffer_id
135+
order by start_cost;
136+
137+
-- get path geometry from a given location to a buffer center
138+
with
139+
part1 as( -- get links of the path
140+
select link_id from sf_nbl$
141+
where buffer_id = 1
142+
start with link_id = 799415310
143+
connect by prior prev_link_id = link_id and
144+
prior start_cost = end_cost and
145+
prior buffer_id = buffer_id
146+
order by start_cost
147+
),
148+
part2 as( -- get geometry of each link of the path
149+
select a.link_id, b.geometry
150+
from part1 a, here_sf_net_link$ b
151+
where a.link_id = b.link_id
152+
)
153+
select SDO_AGGR_CONCAT_LINES(b.geometry) path -- create path geometry
154+
from part2 b;
155+
156+
157+
-- get path geometry vertices from a given location to a buffer center
158+
with
159+
part1 as( -- get links of the path
160+
select link_id from sf_nbl$
161+
where buffer_id = 1
162+
start with link_id = 799415310
163+
connect by prior prev_link_id = link_id and
164+
prior start_cost = end_cost and
165+
prior buffer_id = buffer_id
166+
order by start_cost
167+
),
168+
part2 as( -- get geometry of each link of the path
169+
select a.link_id, b.geometry
170+
from part1 a, here_sf_net_link$ b
171+
where a.link_id = b.link_id
172+
),
173+
part3 as (
174+
select SDO_AGGR_CONCAT_LINES(b.geometry) path -- create path geometry
175+
from part2 b
176+
)
177+
select t.x,t.y,t.id from
178+
part3 a, table(sdo_util.getvertices(a.path)) t -- create path geometry
179+
order by t.id ;
180+
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package lod.networkbuffer;
2+
3+
import java.io.InputStream;
4+
import java.sql.Connection;
5+
import java.sql.PreparedStatement;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
import java.text.DecimalFormat;
10+
import java.text.NumberFormat;
11+
import oracle.spatial.network.lod.NetworkAnalyst;
12+
import oracle.spatial.network.lod.LODNetworkManager;
13+
import oracle.spatial.network.lod.LinkCostCalculator;
14+
import oracle.spatial.network.lod.NetworkIO;
15+
import oracle.spatial.network.lod.PointOnNet;
16+
import oracle.spatial.network.lod.util.PrintUtility;
17+
import oracle.spatial.util.Logger;
18+
import oracle.spatial.network.lod.NetworkBuffer;
19+
import oracle.spatial.network.lod.NodeCostCalculator;
20+
import oracle.spatial.network.lod.config.ConfigManager;
21+
import oracle.spatial.network.lod.config.LODConfig;
22+
23+
public class PersistentNetworkBuffer
24+
{
25+
private static final NumberFormat formatter = new DecimalFormat("#.######");
26+
27+
private static NetworkAnalyst analyst;
28+
private static NetworkIO networkIO;
29+
30+
private static void setLogLevel(String logLevel)
31+
{
32+
if("FATAL".equalsIgnoreCase(logLevel))
33+
Logger.setGlobalLevel(Logger.LEVEL_FATAL);
34+
else if("ERROR".equalsIgnoreCase(logLevel))
35+
Logger.setGlobalLevel(Logger.LEVEL_ERROR);
36+
else if("WARN".equalsIgnoreCase(logLevel))
37+
Logger.setGlobalLevel(Logger.LEVEL_WARN);
38+
else if("INFO".equalsIgnoreCase(logLevel))
39+
Logger.setGlobalLevel(Logger.LEVEL_INFO);
40+
else if("DEBUG".equalsIgnoreCase(logLevel))
41+
Logger.setGlobalLevel(Logger.LEVEL_DEBUG);
42+
else if("FINEST".equalsIgnoreCase(logLevel))
43+
Logger.setGlobalLevel(Logger.LEVEL_FINEST);
44+
else //default: set to ERROR
45+
Logger.setGlobalLevel(Logger.LEVEL_ERROR);
46+
}
47+
48+
private static boolean tableExists(Connection conn, String tableName)
49+
{
50+
boolean result = false;
51+
try {
52+
Statement stmt = conn.createStatement();
53+
String sqlStr = "SELECT COUNT(*) FROM TAB WHERE TNAME = '" + tableName.toUpperCase() + "'";
54+
ResultSet rs = stmt.executeQuery(sqlStr);
55+
56+
if ( rs.next() ) {
57+
int no = rs.getInt(1);
58+
if ( no != 0 )
59+
result = true;
60+
}
61+
rs.close();
62+
stmt.close();
63+
}
64+
catch (Exception e) {
65+
e.printStackTrace();
66+
}
67+
return result;
68+
}
69+
70+
public static void main(String[] args) throws Exception
71+
{
72+
73+
System.out.println("\n\ncreate reaching buffer\n\n");
74+
75+
String configXmlFile = "lod/networkbuffer/LODConfigs.xml";
76+
String logLevel = "ERROR";
77+
78+
String dbUrl = ""; // jdbc url
79+
String dbUser = "";
80+
String dbPassword = "";
81+
82+
String networkName = "HERE_SF_NET";
83+
84+
long startNodeId = 48523065;
85+
long linkId = 947224640;
86+
double percent = 0;
87+
88+
long[] linkIds = {915260080, 711576509, -23618421, -127806843, 23618590, 23595880, 23594646, 23748128, 23611433, -127806839, 23612874, -916623909};
89+
double[] percents = {0.2, 0.48, 0.27, 0.72, 0.63, 0.14, 0.1, 1, 0.29, 0.14, 0, 0};
90+
91+
92+
int linkLevel = 1; //default link level
93+
// double cost = 10*1600; // 10 miles converted to meters
94+
double cost = 10*60; // 10 minutes converted to seconds
95+
String tableNamePrefix = "SF";
96+
97+
Connection conn = null;
98+
99+
//get input parameters
100+
for(int i=0; i<args.length; i++)
101+
{
102+
if(args[i].equalsIgnoreCase("-dbUrl"))
103+
dbUrl = args[i+1];
104+
else if(args[i].equalsIgnoreCase("-dbUser"))
105+
dbUser = args[i+1];
106+
else if(args[i].equalsIgnoreCase("-dbPassword"))
107+
dbPassword = args[i+1];
108+
else if(args[i].equalsIgnoreCase("-networkName") && args[i+1]!=null)
109+
networkName = args[i+1].toUpperCase();
110+
else if(args[i].equalsIgnoreCase("-linkLevel"))
111+
linkLevel = Integer.parseInt(args[i+1]);
112+
else if(args[i].equalsIgnoreCase("-startNodeId"))
113+
startNodeId = Long.parseLong(args[i+1]);
114+
else if(args[i].equalsIgnoreCase("-cost:"))
115+
cost = Double.parseDouble(args[i]);
116+
else if(args[i].equalsIgnoreCase("-tableNamePrefix"))
117+
tableNamePrefix = args[i+1];
118+
else if(args[i].equalsIgnoreCase("-configXmlFile"))
119+
configXmlFile = args[i+1];
120+
else if(args[i].equalsIgnoreCase("-logLevel"))
121+
logLevel = args[i+1];
122+
}
123+
124+
// opening connection
125+
conn = LODNetworkManager.getConnection(dbUrl, dbUser, dbPassword);
126+
127+
Statement stmt = conn.createStatement();
128+
129+
System.out.println("Network analysis for "+networkName);
130+
131+
setLogLevel(logLevel);
132+
133+
//load user specified LOD configuration (optional),
134+
//otherwise default configuration will be used
135+
InputStream config = ClassLoader.getSystemResourceAsStream(configXmlFile);
136+
LODNetworkManager.getConfigManager().loadConfig(config);
137+
LODConfig c = LODNetworkManager.getConfigManager().getConfig(networkName);
138+
//get network input/output object
139+
networkIO = LODNetworkManager.getCachedNetworkIO(
140+
conn, networkName, networkName, null);
141+
142+
//get network analyst
143+
analyst = LODNetworkManager.getNetworkAnalyst(networkIO);
144+
LinkCostCalculator[] oldlccs = analyst.getLinkCostCalculators();
145+
LinkCostCalculator[] lccs = {new LinkTravelTimeCalculator()};
146+
try
147+
{
148+
149+
150+
analyst.setLinkCostCalculators(lccs);
151+
152+
153+
for (int i = 0; i < linkIds.length; i++) {
154+
int bufferId = i + 1;
155+
System.out.println("***** BEGIN: Network Buffer " + bufferId + " *****");
156+
157+
linkId = linkIds[i];
158+
percent = percents[i];
159+
160+
PointOnNet[] startPoint = {new PointOnNet(linkId, percent)};
161+
long startTime = System.currentTimeMillis();
162+
NetworkBuffer buffer = analyst.reachingNetworkBuffer(startPoint, cost, null);
163+
long t1 = System.currentTimeMillis();
164+
networkIO.writeNetworkBuffer(buffer, bufferId, tableNamePrefix);
165+
166+
167+
long endTime = System.currentTimeMillis();
168+
169+
System.out.println("Run times : "+" Total = "+(endTime-startTime) +" msec."+
170+
" Analysis = "+(t1-startTime)+" msec."+" Persistence = "+
171+
(endTime-t1)+" msec.");
172+
System.out.println("***** END: Network Buffer" + bufferId + " *****");
173+
}
174+
175+
176+
177+
}
178+
catch (Exception e)
179+
{
180+
e.printStackTrace();
181+
}
182+
183+
184+
if(conn!=null)
185+
try{conn.close();} catch(Exception ignore){}
186+
187+
}
188+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Create Drive Time Areas and Perform Drive Time Distance Analysis
2+
3+
Java Example and SQL scripts for [AskTom session on Oct 20, 2020](https://asktom.oracle.com/pls/apex/asktom.search?oh=10082)
4+
5+
The Java example illustrates the generation and persistence of network buffers in the database with the Network Data Model (NDM) Java API.
6+
The SQL queries illustrate the workflow with network buffers in the tutorial.
7+
8+
## Components
9+
10+
PersistentNetworkBuffers.java: Java example for network buffer generation and persistence to the database
11+
12+
AskTom_scripts.sql: SQL scripts for geocoding, reverse geocoding, queries of the min. cost and shortest path geometry with network buffers
13+
14+
## Resources
15+
16+
Spatial Features Homepage: https://oracle.com/goto/spatial
17+
18+
NDM Demo Homepage: https://oracle.com/downloads/samplecode/ndm-graph-samplecode-downloads.html

0 commit comments

Comments
 (0)