xpp_A_doc

Embed Size (px)

Citation preview

  • 8/12/2019 xpp_A_doc

    1/142

    Appendix APractices and Solutions

  • 8/12/2019 xpp_A_doc

    2/142

  • 8/12/2019 xpp_A_doc

    3/142

    Oracle Spatial 11g: Essentials A - 3

    Practices for Lesson 1

    In this practice, you answer questions to recapitulate Oracle Spatial concepts and

    terminologies discussed in the lesson.

  • 8/12/2019 xpp_A_doc

    4/142

    Oracle Spatial 11g: Essentials A - 4

    Practice 1-1: Overview of Oracle Spatial Concepts

    1) State true or false for the following statements about the Points geometry:

    i) A 2D point defines a location in X and Y coordinates.

    ii) A point may be used to represent the location of a building or an automated

    teller machine (ATM).Answer:

    a) i)True

    b) ii)True

    2) State true or false for the following statements about the Line Strings geometry:

    i) Self-crossing line strings are invalid.

    ii) When line strings close to form a ring, they have an area.

    iii) The boundary of a line string is defined by its end points.

    Answer:

    a) i) False

    b) ii) False

    c) iii) True

    3) State true or false for the following statements about the Polygons geometry:

    i) Polygons have an area.

    ii) Self-crossing polygons are valid and supported in Oracle Spatial

    Answer:

    a) i) True

    b) ii) False

    4) Define an optimized rectangle.

    Answer:An optimized rectangle is a polygon represented by the lower-left point and the

    upper-right point of the rectangle.

    5) Answer whether the statement is true or false:

    i) An outer ring polygon is defined in the counterclockwise direction. Answer:

    True

    ii) Inner rings must be followed by its outer ring. Answer: False

    iii) A geometry must be composed of the same element types. Answer: False

    iv) An Oracle tables column of the SDO_GEOMETRYtype is defined as a spatiallayer. Answer: True

    v) A spatial layer can contain different types of geometries. Answer: True

    vi) Tolerance defines the precision of spatial data. Answer: True

  • 8/12/2019 xpp_A_doc

    5/142

    Practice 1-1: Overview of Oracle Spatial Concepts (continued)

    Oracle Spatial 11g: Essentials A - 5

    6) Fill in the blank spaces:

    i) A rectangle that minimally encloses a geometry is called a_____________________(Answer:minimum bounding rectangle)

    ii) An optimized circle is a polygon defined by_____________distinct points on

    the circumference of the circle. (Answer:three)

    iii) The primary filter uses the ________________ to compare geometry

    approximations to get a superset of the result. (Answer:spatial index)

    7) Match the following:

    Term Definition

    a) Geocoding a) Associates a measure value with each

    2D or 3D point along a linear feature

    b) Linear Referencing System b) Provides routing information such as

    driving distances, or directions between

    two locations

    c) Routing c) Associates latitude and longitude

    coordinates with postal addresses

    Answer: The term and definition matched pairs are: (a, c), (b, a), (c, b).

  • 8/12/2019 xpp_A_doc

    6/142

    Oracle Spatial 11g: Essentials A - 6

    Practices for Lesson 2

    In Part 1 of this practice, you create a table, geom_data, with a spatial column andinsert different types of geometries in the table.In Part 2 of this practice, you create some more tables for storing spatial data. The data in

    these tables will be loaded later in Practice 5-1. You also update the

    USER_SDO_GEOM_METADATAview.

  • 8/12/2019 xpp_A_doc

    7/142

    Oracle Spatial 11g: Essentials A - 7

    Practice 2-1: Creating Spatial Layers

    Part 1:

    1) Start SQL*Plus and log in using student/studentas the username/password.

    Answer:

    a) On your desktop, double-click the SQL*Plus icon.

    b) Enter the following command:

    conn student/student.

    2) Describe the SDO_GEOMETRYdata type and review its attributes and memberfunctions.

    Answer:

    a) At the SQL*Plus prompt, enter the following command:

    DESCRIBE SDO_GEOMETRY

    b) The output is shown in the following screenshots:

  • 8/12/2019 xpp_A_doc

    8/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 8

    3) Create a table, geom_data, with the following structure:

    i) Object_id number PRIMARY KEY

    ii) Name varchar2(30)

    iii) Shape SDO_GEOMETRY

  • 8/12/2019 xpp_A_doc

    9/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 9

    Answer:

    a) Enter the following code to create the geom_datatable:

    CREATE TABLE geom_data (object_id number primary key,name varchar2(30),

    shape SDO_GEOMETRY);

    4) Insert a two-dimensional point with the following coordinates and nonspatial attribute

    values. Use the SDO_POINTfield. Set SRIDto NULL.

    i) Object_id:1

    ii) Name:Point

    iii) Coordinates:X=12,Y=14

    Answer:To insert a point by using the SDO_POINTfield, enter the following INSERTstatement:

    INSERT INTO geom_data VALUES(1, 'Point',SDO_GEOMETRY( 2001, NULL,

    SDO_POINT_TYPE(12, 14, NULL),NULL,

    NULL));

  • 8/12/2019 xpp_A_doc

    10/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 10

    5) Insert a line string composed of straight lines by using the following values:

    i) Object_id:2

    ii) Name:Line String

    iii) Coordinates:(10,25), (20,30), (25,25), (30,30)

    iv) Set SRIDto NULL.

    Answer:To insert a line string, enter the following command:

    INSERT INTO geom_data VALUES (2,'Line String',

    SDO_GEOMETRY (2002, NULL, NULL,SDO_ELEM_INFO_ARRAY (1,2,1),SDO_ORDINATE_ARRAY (10,25, 20,30, 25,25, 30,30))

    );

    6) Insert an optimized rectangle by using the following values:

    i) Object_id: 3

    ii) Name:Rectangle

    iii) Coordinates:(1,1), (5,7)

    iv) Set SRIDto NULL.Answer:To insert an optimized rectangle, enter the following command:

    INSERT INTO geom_data VALUES(3,'Rectangle',

    SDO_GEOMETRY(2003, NULL, NULL,SDO_ELEM_INFO_ARRAY(1,1003, 3),SDO_ORDINATE_ARRAY(1,1, 5,7))

    );

  • 8/12/2019 xpp_A_doc

    11/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 11

    7) Insert a polygon with a hole by using the following values:

    i) Object_id: 4

    ii) Name:Polygon with a hole

    iii) Coordinates:

    (1) Outer ring polygon: (2,4), (4,3), (10,3), (13,5), (13,9),(11,13), (5,13), (2,11), (2,4)

    (2) Inner ring polygon: (7,5), (7,10), (10,10), (10,5), (7,5)

    Answer:To insert a polygon with a hole by using the given coordinates, enter the

    following command:

    INSERT INTO geom_data VALUES(4,'Polygon with a hole',

    SDO_GEOMETRY(2003, NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1, 19,2003,1),SDO_ORDINATE_ARRAY(2,4, 4,3, 10,3, 13,5, 13,9, 11,13,

    5,13, 2,11, 2,4,7,5, 7,10, 10,10, 10,5, 7,5)));

    If you want an extra challenge, complete the following exercise:8) Insert a compound polygon with the following values:

    i) Object_id: 5

    ii) Name: Compound Polygon

    iii) Shape:

  • 8/12/2019 xpp_A_doc

    12/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 12

    Answer: To insert the compound polygon, enter the following INSERTstatement:

    INSERT INTO geom_data VALUES(5, 'Compound Polygon',

    SDO_GEOMETRY(2003, NULL, NULL,

    SDO_ELEM_INFO_ARRAY(1,1005,2, 1,2,1, 5,2,2),SDO_ORDINATE_ARRAY(6,10, 10,1, 14,10, 10,14, 6,10)));

    Part 2:

    9) Create the following tables:

    i) GEOD_CITIES

    Column name Data type DescriptionLOCATION SDO_GEOMETRY City locationCITY VARCHAR2(42) City nameSTATE_ABRV VARCHAR2(2) State codePOP90 NUMBER Population (1990)

    RANK90 NUMBER Population ranking (1990)

    ii) GEOD_INTERSTATES

    Column name Data type DescriptionHIGHWAY VARCHAR2(35) Interstate nameGEOM SDO_GEOMETRY Interstate geometry

  • 8/12/2019 xpp_A_doc

    13/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 13

    Answer: To create the tables as described, enter and run the following code:

    CREATE TABLE GEOD_CITIES(LOCATION SDO_GEOMETRY,CITY VARCHAR2(42),STATE_ABRV VARCHAR2(2),POP90 NUMBER,

    RANK90 NUMBER);

    CREATE TABLE GEOD_INTERSTATES(HIGHWAY VARCHAR2(35),GEOM SDO_GEOMETRY);

    10) Describe the structure of the USER_SDO_GEOM_METADATAview.

    Answer:To view the structure of the USER_SDO_GEOM_METADATAview, enter the

    following command:DESCRIBE USER_SDO_GEOM_METADATA

  • 8/12/2019 xpp_A_doc

    14/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 14

    11) Insert metadata for the following layers into the USER_SDO_GEOM_METADATAview:

    i) GEOM_DATA (SHAPE):

    (1) Set SRIDto NULL.

    (2) Set the tolerance to 0.005.(3) For SDO_DIM_ARRAY, set the x-axis in the range from 0 to 300 and

    y-axis in the range from 0 to 300.

    ii) GEOD_CITIES (LOCATION):

    (1) Set SRIDto 8307.

    (2) Set the tolerance to 0.05.

    (3) For SDO_DIM_ARRAY, set the Longaxis in the range from 180.0 to180.0 and the Lataxis in the range from 90.0 to 90.0.

    iii) GEOD_INTERSTATES (GEOM):(1) Set SRIDto 8307.

    (2) Set the tolerance to 0.05.

    (3) For SDO_DIM_ARRAY, set the Longaxis in the range from 180.0 to180.0 and the Lataxis in the range from 90.0 to 90.0.

    Note:Spatial data is loaded into the GEOD_CITIESand GEOD_INTERSTATEStables in the lesson titled, Loading Spatial Data. Make sure you commit the

    inserted rows.

    Answer:To individually update the USER_SDO_GEOM_METADATAview with detailsabout each table, enter and run the following:

    INSERT INTO user_sdo_geom_metadata(TABLE_NAME, COLUMN_NAME,DIMINFO, SRID) VALUES ('GEOM_DATA','SHAPE',

    SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', 0, 300, 0.005),SDO_DIM_ELEMENT('Y', 0, 300, 0.005)

    ),NULL);

    INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME,DIMINFO, SRID) VALUES ('GEOD_CITIES','LOCATION',

    SDO_DIM_ARRAY(SDO_DIM_ELEMENT('LONG', -180.0, 180.0, 0.05),SDO_DIM_ELEMENT('LAT', -90.0, 90.0, 0.05)),

    8307);

  • 8/12/2019 xpp_A_doc

    15/142

    Practice 2-1: Creating Spatial Layers (continued)

    Oracle Spatial 11g: Essentials A - 15

    INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME,DIMINFO, SRID) VALUES('GEOD_INTERSTATES','GEOM',

    SDO_DIM_ARRAY(

    SDO_DIM_ELEMENT('LONG', -180.0, 180.0, 0.05),SDO_DIM_ELEMENT('LAT', -90.0, 90.0, 0.05)),

    8307);commit;

  • 8/12/2019 xpp_A_doc

    16/142

    Oracle Spatial 11g: Essentials A - 16

    Practices for Lesson 3

    In this practice, you insert collection geometries in the geom_datatable. You also usesome of the SDO_GEOMETRYmember methods and constructors.

    Prerequisite:To successfully perform this practice, you must have completed theprevious practice. If you did not perform the previous practice for lesson 2 completely,start a new SQL* Plus window and run the

    d:\labs\catchup_scripts\prac_02.sqlfile.

  • 8/12/2019 xpp_A_doc

    17/142

    Oracle Spatial 11g: Essentials A - 17

    Practice 3-1: Defining Collection Geometries

    1) Insert an oriented point in the geom_datatable by using the following values:

    i) Object_id:6

    ii) Name: Oriented Point

    iii) Coordinates:(12,14)

    iv) Orientation vector values:(0.3, 0.2)

    Answer: To insert an oriented point, enter the following INSERTstatement:

    INSERT INTO geom_data VALUES(6, 'Oriented Point',

    SDO_GEOMETRY(2001, NULL, NULL,SDO_ELEM_INFO_ARRAY(1,1,1, 3,1,0),SDO_ORDINATE_ARRAY(12,14, 0.3,0.2))

    );

    2) Insert a multiline geometry composed of two line strings. Use the following values:

    i) Object_id:7

    ii) Name:Multiline

    iii) Coordinates:(50,22, 60,22) (65,20, 65,25)

    Answer: To insert a multiline, enter the following code:

    INSERT INTO geom_data VALUES (7,'Multiline',

    SDO_GEOMETRY (2006, NULL, NULL,SDO_ELEM_INFO_ARRAY (1,2,1, 5,2,1),SDO_ORDINATE_ARRAY (50,22, 60,22, 65,20, 65,25))

    );

  • 8/12/2019 xpp_A_doc

    18/142

    Practice 3-1: Defining Collection Geometries (continued)

    Oracle Spatial 11g: Essentials A - 18

    3) Insert a multipolygon geometry composed of two rectangles touching at one point.

    Use the following values:

    i) Object_id:8

    ii) Name:Multipolygon-touching

    iii) Coordinates:(50,115, 65,125, 65,125, 75,130)

    Answer: To insert a multipolygon, enter the following code:

    INSERT INTO geom_data VALUES (8,'Multipolygon touching',

    SDO_GEOMETRY (2007, NULL, NULL,SDO_ELEM_INFO_ARRAY (1,1003,3, 5,1003,3),SDO_ORDINATE_ARRAY (50,115, 65,125, 65,125, 75,130))

    );

    4) Write a query to get the dimension and the GTYPEof the geometry object withObject_id as 4.

    Answer: To get the dimension and GTYPE, enter the following code:

    SELECT g.shape.Get_Dims(), g.shape.Get_Gtype()FROM geom_data gWHERE g.object_id = 4;

  • 8/12/2019 xpp_A_doc

    19/142

    Practice 3-1: Defining Collection Geometries (continued)

    Oracle Spatial 11g: Essentials A - 19

    5) Write a query to get the Well-Known Text format of the geometry object with

    Object_id as 5.Note:In the SQL prompt, run set long 150to see the full output.

    Answer:To get the Well-Known Text format, enter the following code:

    SELECT g.shape.Get_WKT()FROM geom_data gWHERE g.object_id = 5;

    If you want extra challenge, complete the following exercise:

    6) Insert a point geometry by using a Well-Known Text string constructor into the

    geom_datatable. Set object_id to 9and Name to Point. The point coordinates

    are (80, 40).

    Answer: To insert a point by using the Well-Known Text string constructor, enter the

    following code:

    INSERT INTO geom_data VALUES (9, 'Point',SDO_GEOMETRY('POINT(80 40)'));

  • 8/12/2019 xpp_A_doc

    20/142

    Oracle Spatial 11g: Essentials A - 20

    Practices for Lesson 4

    In this practice, you run coordinate system transformation routines on geometries. The

    routines transform the geometries into a new coordinate system.

    Prerequisite:This practice does not use any of the database objects created in the

    previous practices. So you can perform this practice even if you did not finish theprevious practices.

  • 8/12/2019 xpp_A_doc

    21/142

    Oracle Spatial 11g: Essentials A - 21

    Practice 4-1: Associating Spatial Layers with CoordinateSystems

    1) Describe the CS_SRStable.

    Answer:Enter the following command:

    DESCRIBE CS_SRS

    2) Run the lab_04_02.sqlscript located in the D:\labs\labsfolder. This scriptcreates the geom_shapestable and inserts two geometries with the SRID8307.

    Answer: Run the lab_04_02.sqlscript at the SQL prompt:

    @d:\labs\labs\lab_04_02.sql

    3) View the two inserted geometries. The spatial column in the table, geom_shapes,is shape.

    Answer:Run the following query:

    SELECT shape FROM geom_shapes;

  • 8/12/2019 xpp_A_doc

    22/142

    Practice 4-1: Associating Spatial Layers with CoordinateSystems (continued)

    Oracle Spatial 11g: Essentials A - 22

    4) Insert a new row for geom_shapes(shape)in theUSER_SDO_GEOM_METADATAview.

    i) Set the tolerance to 0.05.

    ii) Set the Longaxis range: 180 to 180.

    iii) Set the Lataxis range: 90 to 90.

    iv) Set SRIDto 8307.

    Answer: Insert the spatial layer details in theUSER_SDO_GEOM_METADATAview:

    INSERT INTO user_sdo_geom_metadata(TABLE_NAME, COLUMN_NAME, DIMINFO, SRID) VALUES('geom_shapes', 'shape',SDO_DIM_ARRAY(SDO_DIM_ELEMENT('Longitude', -180, 180, 0.05),SDO_DIM_ELEMENT('Latitude', -90, 90, 0.05)

    ),8307);COMMIT;

    5) Transform the geometry with Object_id as 1in the geom_shapestable to 32618(WGS 84/UTM zone 18N).

    Note:In this transformation, the original geometry is not actually transformed. The

    transformed geometry is displayed as a result.

    Answer:Enter the following query:

    SELECT SDO_CS.TRANSFORM(g.shape, 0.05, 32618)

    FROM geom_shapes gWHERE g.object_id=1;

  • 8/12/2019 xpp_A_doc

    23/142

    Practice 4-1: Associating Spatial Layers with CoordinateSystems (continued)

    Oracle Spatial 11g: Essentials A - 23

    6) Transform the entire shapelayer and put results in the table namedgeom_shapes_CS_32618.

    Note:This transformation procedure actually transforms the geometries into the new

    coordinate system and stores the results in the new table,

    geom_shapes_CS_32618. The new table is created by the procedure itself.Answer: Run the following statement at the SQL prompt:

    CALL SDO_CS.TRANSFORM_LAYER('geom_shapes','shape','geom_shapes_CS_32618',32618);

    7) Describe the geom_shapes_CS_32618table.

    Answer:Run the following command:

    DESCRIBE geom_shapes_CS_32618

    8) Select all the geometries from the geom_shapes_CS_32618table.

    Answer: Run the following query:

    SELECT geometry FROM geom_shapes_CS_32618;

  • 8/12/2019 xpp_A_doc

    24/142

    Oracle Spatial 11g: Essentials A - 24

    Practices for Lesson 5

    In this practice, first, you load the GEOD_CITIESand GEOD_INTERSTATEStables byusing SQL*Loader. The respective .ctland .datfiles are located in theD:\labs\datafolder. Then you import spatial data from the Data Pump import files,geod_counties.dmpand proj_data.dmp, located in the d:\labs\datafolder. Finally, you use the SampleShapefileToJGeomFeatureutility to processa STATESshapefile.Prerequisite:To successfully perform this practice, you must have completed the

    practice for the lesson titled Creating Spatial Layers.

    If you did not perform any of the previous practices and you want to bring up yourschema to the desired state, start a new SQL* Plus window and run the SQL script,

    d:\labs\catchup_scripts\prac_0203.sql.

  • 8/12/2019 xpp_A_doc

    25/142

    Oracle Spatial 11g: Essentials A - 25

    Practice 5-1: Loading Spatial Data

    Loading spatial data by using SQL*Loader

    1) The GEOD_CITIESand GEOD_INTERSTATEStables were created in a previousexercise. You now perform the following tasks:

    i) Load the GEOD_CITIEStable by using the geod_cities.datandgeod_cities.ctlfiles.

    ii) Load the GEOD_INTERSTATEStable by using thegeod_interstates.datand geod_interstates.ctlfiles.

    iii) The respective .datand .ctlfiles are located in the D:\labs\datafolder. Invoke sqlldrto load data in the two tables.

    Answer: To invoke SQL*Loader, open a command prompt window.

    a) In the command prompt window, change to the D:\labs\datafolder. Enter

    the following command at the command prompt:sqlldr student/student control=geod_cities.ctldata=geod_cities.dat direct=true

    Note:If you do not want to enter the command, you can run the

    d:\labs\soln\sol_05_01_a.batfile to accomplish the same task.

    b) Similarly, load the data in the geod_interstatestable by using thegeod_interstates.ctl and geod_interstates.datfiles.

    sqlldr student/student control=geod_interstates.ctl data=geod_interstates.dat direct=true

  • 8/12/2019 xpp_A_doc

    26/142

    Practice 5-1: Loading Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 26

    Note:If you do not want to enter the command, you can run the

    d:\labs\soln\sol_05_01_b.batfile to accomplish the same task.

    Loading spatial data by using Data Pump import

    2) Import the GEOD_COUNTIEStable from the geod_counties.dmpfile. Use DataPump import. The geod_counties.dmpfile is in the D:\labs\datafolder.

    Note:Before you can use the impdpcommand to import the geod_countiestable,log in using system/oracleas the username/password and then create a directoryobject named student_dumpdir, using the CREATEDIRECTORYstatement. Thesystemuser must grant read and write privileges on the directory,student_dumpdir, to the user student. You can create a log file namedgeod_counties.logand save it in the D:\labs\datafolder, just in case youwant to monitor errors if any.

    Answer: Perform the following steps:

    a) First, log in using system/oracleas the username/password. Create adirectory by entering the following SQL statement at the SQL prompt:

    conn system/oracle

    CREATE DIRECTORY student_dumpdir as 'D:\labs\data';b) Grant read and write privileges to student.

    grant read, write on directory student_dumpdir to student;

    c) Open a command window and then use the impdpcommand to import thegeod_counties.dmpfile.

    impdp student/student dumpfile =student_dumpdir:geod_counties.dmplogfile = student_dumpdir:geod_counties.log

  • 8/12/2019 xpp_A_doc

    27/142

    Practice 5-1: Loading Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 27

    Note:If you do not want to enter the command, you can run the

    d:\labs\soln\sol_05_02_c.batfile to import the geod_counties.dmpfile.

    3) In SQL*Plus, connect again as the studentuser. Describe the GEOD_COUNTIEStable and also check the number of records in the table.

    Answer: Run the following queries:

    conn student/studentDESCRIBE geod_counties;SELECT count(*) from geod_counties;

    4) Insert metadata for the GEOD_COUNTIES(GEOM)layer into theUSER_SDO_GEOM_METADATAview. Commit the record.

    i) Set the tolerance to 0.05.

    ii) Set the SRIDto 8307.

    iii) Set the Longaxis range to 180 to 180.

    iv) Set the Lataxis range to 90 to 90.

  • 8/12/2019 xpp_A_doc

    28/142

    Practice 5-1: Loading Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 28

    Answer: Run the following INSERTstatement:

    INSERT INTO USER_SDO_GEOM_METADATA(TABLE_NAME, COLUMN_NAME, DIMINFO, SRID)VALUES('GEOD_COUNTIES', 'GEOM',

    SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', -180.0, 180.0, 0.05),SDO_DIM_ELEMENT('Y', -90.0, 90.0, 0.05)

    ),8307);commit;

    5) Import the following tables from the proj_data.dmpfile:

    i) PROJ_CITIES

    ii) PROJ_INTERSTATES

    iii) PROJ_COUNTIES

    iv) PROJ_STATESNote:Locate the proj_data.dmpfile in the D:\labs\datafolder. Use thesame student_dumpdirdirectory for the import. Generate aproj_data.logfile.

    Answer: Run the following command at the command prompt:

    impdp student/student dumpfile = student_dumpdir:proj_data.dmplogfile = student_dumpdir:proj_data.log

  • 8/12/2019 xpp_A_doc

    29/142

    Practice 5-1: Loading Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 29

    Note:If you do not want to enter the command, you can run the

    d:\labs\soln\sol_05_05.batfile to import the proj_data.dmpfile.

    6) Insert metadata for the following layers:

    i) PROJ_CITIES(LOCATION)

    ii) PROJ_INTERSTATES(GEOM)

    iii) PROJ_COUNTIES(GEOM)

    iv) PROJ_STATES(GEOM)

    Note:Use the following details for all the tables:

    i) X,11000000, 4000000,0.05

    ii) Y, 80000,7000000,0.05

    iii) SRID:32775

    Answer: Run the following INSERTstatements:

    INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME,DIMINFO, SRID)

    VALUES ('PROJ_CITIES', 'LOCATION',SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', -11000000, 4000000, 0.05),SDO_DIM_ELEMENT('Y', -80000, 7000000, 0.05)

    ),32775);

    INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME,DIMINFO, SRID)

    VALUES ('PROJ_COUNTIES', 'GEOM',SDO_DIM_ARRAY

  • 8/12/2019 xpp_A_doc

    30/142

    Practice 5-1: Loading Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 30

    (SDO_DIM_ELEMENT('X', -11000000, 4000000, 0.05),SDO_DIM_ELEMENT('Y', -80000, 7000000, 0.05)

    ),32775);

    INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME,

    DIMINFO, SRID)VALUES ('PROJ_INTERSTATES', 'GEOM',SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', -11000000, 4000000, 0.05),SDO_DIM_ELEMENT('Y', -80000, 7000000, 0.05)

    ),32775);

    INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME,DIMINFO, SRID)

    VALUES ('PROJ_STATES', 'GEOM',SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', -11000000, 4000000, 0.05),SDO_DIM_ELEMENT('Y', -80000, 7000000, 0.05)

    ),32775);

    COMMIT;

    Using the Shapefile Converter Utility

    7) Run the Java sample SampleShapefileToJGeomFeatureutility on theSTATESshapefile provided. Perform the following:

    i) Set the classpath to

    .;%ORACLE_HOME%\jdbc\lib\ojdbc5.jar;%ORACLE_HOME%\md\jlib\sdoutl.jar;%ORACLE_HOME%\md\jlib\sdoapi.jar.Make sure that ORACLE_HOMEis set before setting the classpath.

    ii) Run the SampleShapefileToJGeomFeatureJava-based tool.

    Note:The SampleShapefileToJGeomFeatureJava utility loads polygonsin the four-digit spatial SDO_GTYPEformat, taking into account polygon rotationand ring ordering.

    iii) Locate the STATESfiles in the D:\labs\data\shapefilefolder.

    Answer: Perform the following step:

    a) Open a command window. Change to the D:\labs\data\shapefilefolder.Set the classpath. If ORACLE_HOMEand OC4J_HOMEare not set, set them too asfollows:

    cd d:\labs\data\shapefileset ORACLE_HOME=d:\app\administrator\product\11.1.0\db_1

  • 8/12/2019 xpp_A_doc

    31/142

    Practice 5-1: Loading Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 31

    setclpath=.;%ORACLE_HOME%\jdbc\lib\ojdbc5.jar;%ORACLE_HOME%\md\jlib\sdoutl.jar;%ORACLE_HOME%\md\jlib\sdoapi.jar

    b) To run the Shapefile converter utility, run the following command at the

    command prompt:java -cp %clpath%oracle.spatial.util.SampleShapefileToJGeomFeature -h localhost-p 1521 -s orcl -u student -d student -t geod_states -f states-r 8307 -g geom

    Note:Alternatively, if you do not want to enter the whole path, the classpath, and the

    Java command, run the sol_05_07.batfile. If your ORACLE_HOMEis in anywaydifferent from what is mentioned in the preceding code box, edit the sol_05_07.batfile before running.

    8) Run the verify_labs.sqlscript to check whether all the data is loaded correctly

    with the correct SDO_GTYPE, SRID, and number of rows. Theverify_labs.sqlscript is located in the D:\labs\labsfolder.

    Answer:Run the following script and check the output:

    @d:\labs\labs\verify_labs.sql

  • 8/12/2019 xpp_A_doc

    32/142

    Practice 5-1: Loading Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 32

    The output must be as shown in the following screenshot:

  • 8/12/2019 xpp_A_doc

    33/142

    Oracle Spatial 11g: Essentials A - 33

    Practices for Lesson 6

    In this practice, you insert invalid geometries and then use validation functions to validate

    them. You also use debugging functions to correct geometries.

    Prerequisite:To successfully complete this practice, you must have completed the

    practices for the lessons titled Creating Spatial Layers and Defining CollectionGeometries. If you did not complete the practices, run

    d:\labs\catchup_scripts\prac_0203.sql.

    Prerequisite for the remaining practices:

    For the remaining practices, it is recommended that your schema is fully loaded with the

    desired tables. If you did not complete the practice for the lesson titled Loading Spatial

    Data, it is important that you perform the following steps to bring your schema to thestate it should be for the remaining practices. Even if this practice is not dependent on

    practice 5-1, it ensures that your practices run successfully for the remaining lessons. Run

    the scripts in the following sequence [all the scripts (.sqland .batfiles) are located inthe d:\labs\catchup_scriptsfolder]:

    i) If you did not complete the practices for the lessons titled Creating Spatial

    Layers and Defining Collection Geometries, open a new SQL*Pluswindow and enter the following command:

    @d:\labs\catchup_scripts\prac_0203.sql

    ii) In Windows Explorer, double-click

    load_geod_cities_interstates.batto run the batch file.

    iii) In SQL*Plus, enter the following command:

    @d:\labs\catchup_scripts\create_directory.sql

    iv) In Windows Explorer, double-click load_geod_proj_data.bat to runit.

    v) In SQL*Plus, enter the following command:

    @d:\labs\catchup_scripts\update_metadata.sql

    vi) In Windows Explorer, double-click the load_geod_states.batfile.

  • 8/12/2019 xpp_A_doc

    34/142

    Oracle Spatial 11g: Essentials A - 34

    Practice 6-1: Validating and Debugging Geometries

    1) Before you use the validation and debugging functions, insert a few invalid

    geometries in the geom_datatable. Run the lab_06_01.sqlscript to insertinvalid geometries in the geom_datatable. The lab_06_01.sqlscript is in theD:\labs\labsfolder.

    Answer:At the SQL prompt, enter the following command:

    @D:\labs\labs\lab_06_01.sql

    2) Validate the geometry with Object_id as 10.

    Answer: Run the following query:

    SELECT SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(g.shape, 0.05)FROM geom_data gWHERE g.object_id = 10;

    Note: The error 13351 has the following description:

    ORA-13351:Two or more rings of a complex polygon overlap.

    Cause:The inner or outer rings of a complex polygon overlap.

    Action:All rings of a complex polygon must be disjoint. Correct the geometric

    definition of the object.

    3) Create a val_resultstable with the following structure:

    Column name Data typesdo_rowid ROWIDresult VARCHAR2(1000)

    Answer: Run the following SQL statement:

    CREATE TABLE val_results(sdo_rowid ROWID,result VARCHAR2(1000)

    );

  • 8/12/2019 xpp_A_doc

    35/142

    Practice 6-1: Validating and Debugging Geometries (continued)

    Oracle Spatial 11g: Essentials A - 35

    4) Validate the geom_data(shape)spatial layer and review the validation results.

    Answer: Perform the following steps:

    a) To validate the entire layer, run the following statement:

    CALL

    SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT('GEOM_DATA','SHAPE','VAL_RESULTS');

    Note:The values in the procedure must be in uppercase.

    b) View the records in the val_resultstable:

    SELECT * FROM val_results;

    Note:Out of the 13 rows processed, four rows have invalid geometries. The

    VAL_RESULTStable contains the ROWIDof the geometry and the associated errornumber and its description in the RESULTcolumn.

    5) Write a query to return the vertices of the geometry object with Object_id as 13.

    Answer: Run the following query:

    SELECT t.X, t.YFROM geom_data g,

    TABLE(SDO_UTIL.GETVERTICES(g.shape)) tWHERE g.Object_id=13;

  • 8/12/2019 xpp_A_doc

    36/142

    Practice 6-1: Validating and Debugging Geometries (continued)

    Oracle Spatial 11g: Essentials A - 36

    Note:Notice the redundant vertices.

    6) Remove redundant vertices from the geometry object with Object_id as 13.

    Answer: Run the following query:

    SELECT sdo_util.rectify_geometry(g.shape, 0.5)FROM geom_data gwhere g.object_id=13;

    7) Debug the geometry object with Object_id as 11. Use the RECTIFY_GEOMETRY

    function. First, verify in the val_resultstable and find out what is wrong withthe geometry.

    Answer: Perform the following steps:

    a) Find out the ROWIDof the geometry so that you can search for the correspondingROWIDin the val_resultstable.

    SELECT ROWID FROM geom_data WHERE object_id=11;

    b) Search the resultcolumn for the same ROWIDand find out the error numberassociated with it.

    SELECT result FROM val_resultsWHERE SDO_ROWID='AAARF8AAEAAAAAOAAJ';

  • 8/12/2019 xpp_A_doc

    37/142

    Practice 6-1: Validating and Debugging Geometries (continued)

    Oracle Spatial 11g: Essentials A - 37

    Note:This ROWIDwill be different for you, so do not copy it from here. If you arerunning the solution script, sol_06_07_b.sql, replace your ROWIDin the query.

    Note:The error description is as follows:

    ORA-13349: The polygon boundary crosses itself.

    Cause: The boundary of a polygon intersects itself.

    Action: Correct the geometric definition of the object.

    c) Now, run the rectify_geometryfunction to debug the geometry.

    SELECT sdo_util.rectify_geometry(g.shape,0.05)FROM geom_data g

    WHERE rowid='AABH8IAAGAAAAAOAAB';

    Note:Replace the ROWIDin your query with the ROWIDof your geometry.

    8) From the geometry object identified by Object_id as 8, extract the first element.

    Answer: Run the following query:

    SELECT sdo_util.extract(g.shape, 1,0)FROM geom_data gWHERE g.object_id=8;

  • 8/12/2019 xpp_A_doc

    38/142

    Oracle Spatial 11g: Essentials A - 38

    Practices for Lesson 7

    In this practice, you install MapViewer. You also view data in MapViewer. For this

    practice, access the zip file called mv10131_qs.ziplocated in theD:\labs\softwarefolder.

    Prerequisite:To perform this practice completely, you must have completed the practicefor the lesson titled Loading Spatial Data successfully. If you did not, perform the steps

    mentioned in the section Prerequisite for the remaining practices: in the practice for

    lesson 6.

  • 8/12/2019 xpp_A_doc

    39/142

    Oracle Spatial 11g: Essentials A - 39

    Practice 7-1: Using the Oracle Application Server MapViewer

    1) Extract the mv10131_qs.zipfile under the rootdirectory on D: drive.

    Note:You can extract the mv10131_qs.zipfile anywhere outside yourORACLE_HOME.

    Answer: Perform the following steps:

    a) From Windows Explorer, double-click mv10131_qs.zipto start WinZip, andextract it to the local disk (the rootdirectory on the D: drive is D:\).

    b) Click the Extract button. Enter D:\in the Extract to field of the pop-up menu.Click Extract. When you extract the file, it creates the

    D:\mv10131_qsdirectory.

    2) Initialize OC4J and deploy MapViewer. Edit the start.batfile in yourD:\mv10131_qsfolder. Deploy MapViewer by running start.bat. Choosewelcomeas the oc4jadminpassword when prompted.

    Answer:Perform the following steps to initialize OC4J and to deploy MapViewer:a) Navigate to the D:\mv10131_qsdirectory.

    b) Right-click start.batand make the following edits:

    i) On the first line, change C:to D:.

    ii) On the second line, change C:to D:.

    iii) Because the bindirectory for Java JDK 1.5 is in your path environmentvariable, on the third line, replace C:\ProgramFiles\Java\jdk1.5.0\bin\java with java.

    c) Save the start.batfile. Exit the editor.d) In Windows Explorer, double-click the start.batfile to run it. A read-only

    console window is displayed. This is where MapViewer reports information and

    error messages.

    e) Choose welcomeas the oc4jadminpassword when prompted. You mustremember this password. In the console window, you should observe the

    following message:

  • 8/12/2019 xpp_A_doc

    40/142

    Practice 7-1: Using the Oracle Application Server MapViewer(continued)

    Oracle Spatial 11g: Essentials A - 40

    INFO [oracle.lbs.mapserver.oms] *** Oracle MapViewerstarted. ***

    Note:Do not close this command window.

    3) Create a dynamic data source. A data source defines the connection information to the

    database for map requests. Define a data source with the Oracle Application Server

    MapViewer Admin JSP.

    Answer:Perform the following steps:

    a) Open Internet Explorer. The Oracle Application Server MapViewer Admin JSP is

    located at: http://localhost:8888/mapviewer.

  • 8/12/2019 xpp_A_doc

    41/142

    Practice 7-1: Using the Oracle Application Server MapViewer(continued)

    Oracle Spatial 11g: Essentials A - 41

    b) On the upper-right corner of the page, click Admin. To perform administrative

    commands, such as adding a data source, enter oc4jadminin the User Namefield and welcomein the Password field. Click Log In.

    c) Under the Management tab, click Datasourcesunder Manage MapViewer, as

    shown in the following screenshot:

    d) In the Create a dynamic data source section, update the fields with the

    following values:

    i) Name: student

    ii) Host: localhost

  • 8/12/2019 xpp_A_doc

    42/142

    Practice 7-1: Using the Oracle Application Server MapViewer(continued)

    Oracle Spatial 11g: Essentials A - 42

    iii) Port: 1521

    iv) Sid: orcl

    v) User: student

    vi) Password: student

    vii) # Mappers: 3

    e) Click Submit. The student data source should be listed under Existing data

    sources.

    4) View the data you loaded in the lesson titled Loading Spatial Data by using

    MapViewer. Select the geometries from the geom_statesand geom_countiestables.

  • 8/12/2019 xpp_A_doc

    43/142

    Practice 7-1: Using the Oracle Application Server MapViewer(continued)

    Oracle Spatial 11g: Essentials A - 43

    Answer: Perform the following steps:

    a) From the MapViewer page, click the Demos tab.

    b) Click the JViewdemo.

    c) When the Simple Spatial Query Visualizer page is displayed, in the top query

    window (query 1:), enter:

    SELECT geom FROM geod_counties

    d) Set the line color to redand the fill to yellow. Enter COUNTYin the Label Columnfield

    e) In the second query window (query 2:), enter:

    SELECT geom FROM geod_states

    Note:Do not end the query with a semicolon.

    f) Set the line color to black and leave the fill empty. Enter STATEin the LabelColumn field

    g) Click the Submit button.

  • 8/12/2019 xpp_A_doc

    44/142

    Practice 7-1: Using the Oracle Application Server MapViewer(continued)

    Oracle Spatial 11g: Essentials A - 44

    h) Zoom in to a location to see more detail.

    Note:Similarly, you can try selecting the locationcolumn from geod_cities,or the geomcolumn from geod_interstates.

    5) Configure the studentdata source. On the MapViewer Admin page, configure adata source that automatically starts when MapViewer is started.

    Answer:Perform the following steps:

    a) Navigate to MapViewer home page. On the upper-right corner of the MapViewer

    page, click Admin.

  • 8/12/2019 xpp_A_doc

    45/142

    Practice 7-1: Using the Oracle Application Server MapViewer(continued)

    Oracle Spatial 11g: Essentials A - 45

    b) Under the Management tab, select Configurationunder Manage MapViewer.

    c) At the bottom of the configuration file, search for the Predefined Data sources

    section. Uncomment the example map_data_sourceelement, and modify thefollowing values:

    i) Name: student

    ii) Host: localhost

    iii) Port: 1521

    iv) Sid: orcl

    v) User: student

    vi) Password: !student

    vii) # Mappers: 3

    d) Click Save & Restart. The student data source should be listed under Existing

    data sources. Note that "!student"causes MapViewer to encrypt thispassword after the first time MapViewer is started with this data source defined.

    You can verify this by looking at the configuration file.

  • 8/12/2019 xpp_A_doc

    46/142

    Oracle Spatial 11g: Essentials A - 46

    Practices for Lesson 8

    In this practice, you create nonspatial and spatial indexes on spatial layers.

    Prerequisite:To perform this practice completely, you must have completed the practicefor the lesson titled Loading Spatial Data successfully. If you did not, perform the steps

    mentioned in the section Prerequisite for the remaining practices in the practice forlesson 6.

  • 8/12/2019 xpp_A_doc

    47/142

    Oracle Spatial 11g: Essentials A - 47

    Practice 8-1: Indexing Spatial Data

    1) Create nonspatial indexes on the following columns. Make sure that all the indexes

    are created on the indx_tblspctablespace already created for you.

    i) GEOD_CITIES(CITY)

    ii) GEOD_STATES(STATE)iii) GEOD_STATES(STATE_ABRV)

    iv) GEOD_INTERSTATES(HIGHWAY)

    v) GEOD_COUNTIES(STATE, COUNTY)

    vi) GEOD_COUNTIES(STATE_ABRV, COUNTY)

    Note:The following nonspatial indexes were created on the projected layers duringimport:

    i) PROJ_CITIES(CITY)ii) PROJ_STATES(STATE)

    iii) PROJ_STATES(STATE_ABRV)

    iv) PROJ_INTERSTATES(HIGHWAY)

    v) PROJ_COUNTIES(STATE, COUNTY)

    vi) PROJ_COUNTIES(STATE_ABRV, COUNTY)

    Answer: To create nonspatial indexes, run the following statements:

    CREATE INDEX geod_cities_idx ON geod_cities(city) TABLESPACE

    indx_tblspc;

    CREATE INDEX geod_counties_idx ON geod_counties(state, county)TABLESPACE indx_tblspc;

    CREATE INDEX geod_counties_abrv_idx ONgeod_counties(state_abrv, county) TABLESPACE indx_tblspc;

    CREATE INDEX geod_states_idx ON geod_states(state) TABLESPACEindx_tblspc;

    CREATE INDEX geod_states_abrv_idx ON geod_states(state_abrv)

    TABLESPACE indx_tblspc;

    CREATE INDEX geod_interstates_idx ON geod_interstates(highway)TABLESPACE indx_tblspc;

  • 8/12/2019 xpp_A_doc

    48/142

    Practice 8-1: Indexing Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 48

    2) Create spatial (R-tree) indexes on the following layers. Again, set the tablespace

    parameter to the indx_tblspctablespace. Also, set the work tablespace parameterto the work_indx_tblspctablespace.

    i) GEOD_CITIES(LOCATION)

    ii) GEOD_INTERSTATES(GEOM)

    iii) GEOD_COUNTIES(GEOM)

    iv) GEOD_STATES(GEOM)

    v) PROJ_CITIES(LOCATION)

    vi) PROJ_STATES(GEOM)

    vii)PROJ_INTERSTATES(GEOM)

    viii) PROJ_COUNTIES(GEOM)

    Answer: Run the following statements to create spatial indexes:

    CREATE INDEX geod_cities_sidx on geod_cities (location)INDEXTYPE IS mdsys.spatial_index

    PARAMETERS ('layer_gtype=point, work_tablespace =work_indx_tblspc, tablespace =indx_tblspc');

    create index GEOD_INTERSTATES_SIDX on GEOD_INTERSTATES (GEOM)indextype is mdsys.spatial_indexparameters ('layer_gtype=multiline, work_tablespace =

    work_indx_tblspc, tablespace =indx_tblspc');

    create index GEOD_COUNTIES_SIDX on GEOD_COUNTIES (GEOM)indextype is mdsys.spatial_indexparameters ('layer_gtype=multipolygon, work_tablespace =

    work_indx_tblspc, tablespace =indx_tblspc');

    create index GEOD_STATES_SIDX on GEOD_STATES (GEOM)indextype is mdsys.spatial_indexparameters ('layer_gtype=multipolygon , work_tablespace

    = work_indx_tblspc, tablespace =indx_tblspc');

    create index PROJ_CITIES_SIDX on PROJ_CITIES (LOCATION)

    indextype is mdsys.spatial_indexparameters ('layer_gtype=point, work_tablespace =work_indx_tblspc, tablespace =indx_tblspc');

    create index PROJ_STATES_SIDX on PROJ_STATES (GEOM)indextype is mdsys.spatial_indexparameters ('layer_gtype=multipolygon, work_tablespace =

    work_indx_tblspc, tablespace =indx_tblspc');

  • 8/12/2019 xpp_A_doc

    49/142

    Practice 8-1: Indexing Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 49

    create index PROJ_INTERSTATES_SIDX on PROJ_INTERSTATES (GEOM)indextype is mdsys.spatial_indexparameters ('layer_gtype=multiline, work_tablespace =

    work_indx_tblspc, tablespace =indx_tblspc');

    create index PROJ_COUNTIES_SIDX on PROJ_COUNTIES (GEOM)

    indextype is mdsys.spatial_indexparameters ('layer_gtype=multipolygon, work_tablespace =work_indx_tblspc, tablespace =indx_tblspc');

    3) Describe USER_SDO_INDEX_METADATA.

    Answer: Run the following command:

    DESCRIBE USER_SDO_INDEX_METADATA

    Note that this view does not have information such as the table name and the columnname on which the index was built.

    4) Describe the USER_SDO_INDEX_INFOview. Write a query to select all the rowsfrom the USER_SDO_INDEX_INFOview. Appropriately set the line size to view theresults in a formatted manner.

    DESCRIBE user_sdo_index_infoSELECT * FROM user_sdo_index_info;

    Note:This view contains information about the table name and column name on

    which the index was built.

    5) Estimate the size of an R-tree index on a table that has 200,000 geometries,

    DB_BLOCK_SIZEset to 16 KB (16384), SDO_RTR_PCTFREEset to 10, two-dimensional data, and a geodetic SRID.

    Answer:Run the following query:

    SELECTsdo_tune.estimate_rtree_index_size(200000 , 16384, 10, 2, 1)FROM dual;

  • 8/12/2019 xpp_A_doc

    50/142

    Practice 8-1: Indexing Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 50

    6) Run the verify_indexes.sqlscript to check whether all the required indexeswere created. The script is located in the d:\labs\labsfolder.

    Answer: Run the script and check the output:

    @d:\labs\labs\verify_indexes.sql

    The output must be as shown in the following screenshot:

    Note:The YP_SIDXindex was imported with the YELLOW_PAGEStable as part ofthe classroom setup. You use this table in a later practice.

  • 8/12/2019 xpp_A_doc

    51/142

    Oracle Spatial 11g: Essentials A - 51

    Practices for Lesson 9

    In this practice, you run spatial queries that apply some spatial operators such as

    SDO_FILTERand SDO_ANYINTERACT. You view the output of some of the queries inthe MapViewer Simple Spatial Query Visualizer. You also run a query that uses the

    SDO_GEOM.RELATEfunction.

    Prerequisite:To perform this practice completely, you must have completed the practice

    for lessons 2, 3, 5, 7, and 8 successfully.

    If you did not complete the practices for lessons 2, 3, and 5, perform the steps mentioned

    in the section titled Prerequisite for the remaining practicesin the practice for lesson 6.For this practice, you should have also completed the practice 7-1 successfully. If not, go

    back to Practice 7-1 and perform the steps.

    To catch up on Practice 8-1, at SQL *Plus prompt, enter the following command:

    @d:\labs\catchup_scripts\create_indexes.sql

  • 8/12/2019 xpp_A_doc

    52/142

    Oracle Spatial 11g: Essentials A - 52

    Practice 9-1: Querying Spatial Data

    1) From SQL*Plus, perform a primary filter query to find all counties that are likely to

    interact with the state of New York. Use the geod_statesand geod_countiestables.

    Answer:Run the following query to find all counties that are likely to interact with the

    state of New York:

    SELECT /*+ ordered */ county, c.state_abrvFROM geod_states s,

    geod_counties cWHERE s.state = 'New York'

    AND sdo_filter (c.geom, s.geom) = 'TRUE';

    ..

    2) This exercise requires that OC4J is running and a connection is properly defined touse MapViewer. If OC4J is not running, follow the directions in Practice 7 to start

    OC4J and define a connection to the database. Use the MapViewer Simple Spatial

    Query Visualizer to see the state of New York, and all counties that are likely tointeract with it.

  • 8/12/2019 xpp_A_doc

    53/142

    Practice 9-1: Querying Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 53

    Answer: Perform the following steps:

    a) Click the start.batbatch file to start the OC4J server.

    b) Open Internet Explorer. Assuming that you have defined the data source, go to the

    URL: http://localhost:8888/mapviewer

    c) Click the Demos tab.

    d) Click jview.jsp. The Simple Spatial Query Visualizer page is displayed.Ensure that the student data source is selected.

    e) In the query 1: window, set the fill color to yellow and enter COUNTYin the LabelColumn field. Enter the following query:

    SELECT /*+ ordered */ c.geomFROM geod_states s, geod_counties cWHERE s.state='New York'

    AND sdo_filter(c.geom, s.geom) = 'TRUE'

    Note:Do not end the query with a semicolon.

    f) In the query 2: window, set the line color to red and enter STATEin the LabelColumn field. Enter the following query:

  • 8/12/2019 xpp_A_doc

    54/142

    Practice 9-1: Querying Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 54

    SELECT geomFROM geod_statesWHERE state='New York'

    g) Click the Submitbutton and view the map.

    3) Find all counties interact with the state of New York.

    Answer: Run the following query:

    SELECT /*+ ordered */ county, c.state_abrvFROM geod_states s,

    geod_counties cWHERE s.state = 'New York'

  • 8/12/2019 xpp_A_doc

    55/142

    Practice 9-1: Querying Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 55

    AND sdo_anyinteract (c.geom, s.geom) = 'TRUE';

    ..

    4) Modify the query you wrote for question 2 to display only the counties that interact

    with the state of New York.

    Hint:Change SDO_FILTERin query 1 to SDO_ANYINTERACT.Answer: Perform the following steps:

    a) Enter the following query in the query1: window:

    SELECT /*+ordered*/ c.geomFROM geod_states s,geod_counties cWHERE s.state='New York'

    AND SDO_ANYINTERACT(c.geom, s.geom) = 'TRUE'

    Note:You can set the fill color to yellow and the label to COUNTY.

    b) In the query 2 window, enter the following query:

    SELECT geomFROM geod_states

    WHERE state='New York'

    Note:You can set the line color to red and the label to STATE.

    c) View the map.

  • 8/12/2019 xpp_A_doc

    56/142

    Practice 9-1: Querying Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 56

    5) Find all cities and the population of the cities in the rectangle that has a lower-left

    coordinate of (109, 37) and an upper-right coordinate of (102, 40). Use the

    geod_citiestable.

    Answer:Run the following query:

    SELECT c.city, c.pop90FROM geod_cities cWHERE sdo_anyinteract(

    c.location,sdo_geometry (

    2003, 8307, null,sdo_elem_info_array (1,1003,3),sdo_ordinate_array (-109, 37, -102, 40))

    )= 'TRUE';

    6) Find all counties and the population of each county that interact with the rectangle

    that has a lower-left coordinate of (109, 37) and an upper-right coordinate of (102,

    40).

  • 8/12/2019 xpp_A_doc

    57/142

    Practice 9-1: Querying Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 57

    Answer: Run the following query:

    SELECT c.county, c.totpopFROM geod_counties cWHERE sdo_anyinteract

    ( c.geom,sdo_geometry (2003, 8307, null,

    sdo_elem_info_array (1,1003,3),sdo_ordinate_array (-109, 37, -102, 40))

    ) = 'TRUE';

    ..

    7) Determine the relationship between the state of New Hampshire and its counties. Set

    the COLrelation toA20.

    Answer:Run the following query:

    SELECT c.county,sdo_geom.relate (s.geom,'DETERMINE', c.geom, 0.5)

    "relation"FROM geod_states s,

    geod_counties cWHERE s.state = 'New Hampshire'

    AND s.state = c.state;

  • 8/12/2019 xpp_A_doc

    58/142

    Practice 9-1: Querying Spatial Data (continued)

    Oracle Spatial 11g: Essentials A - 58

    Note:For formatted output, run colrelation to a20at the SQL prompt.

  • 8/12/2019 xpp_A_doc

    59/142

    Oracle Spatial 11g: Essentials A - 59

    Practices for Lesson 10

    In this practice, you run queries that involve the SDO_NN, SDO_WITHIN_DISTANCE,and SDO_JOINoperators.

    Prerequisite:To perform this practice completely, you must have completed the practicefor lessons 2, 3, 5, 7, and 8 successfully.If you did not complete or partially completed the practices for lessons 2, 3, 5, 7, and 8,

    refer to the Prerequisitesection in the practice for lesson 9.

  • 8/12/2019 xpp_A_doc

    60/142

    Oracle Spatial 11g: Essentials A - 60

    Practice 10-1: Using the SDO_WITHIN_DISTANCE, SDO_NN, and

    SDO_JOINOperators

    1) Find cities within 100 miles of highway 'I10/I5'. Use the geod_interstatesand geod_citiestables.

    Answer:Run the following query:SELECT /*+ ordered */ c.cityFROM geod_interstates i,

    geod_cities cWHERE highway = 'I10/I5'

    AND sdo_within_distance (c.location, i.geom, 'distance=100unit=mile') = 'TRUE';

    2) Find the five nearest cities and their distances nearest to highway 'I170'. Order bydistance in miles.

    Answer: Run the following query:

    SELECT /*+ ORDERED */c.city,sdo_nn_distance (1) distance_in_miles

    FROM geod_interstates i,geod_cities c

    WHERE i.highway = 'I170'AND sdo_nn(c.location, i.geom,

    'sdo_num_res=5 unit=mile', 1) = 'TRUE'ORDER BY distance_in_miles;

  • 8/12/2019 xpp_A_doc

    61/142

    Practice 10-1: Using the SDO_WITHIN_DISTANCE, SDO_NN, andSDO_JOIN Operators (continued)

    Oracle Spatial 11g: Essentials A - 61

    3) Find the five cities nearest to highway 'I170' whose population is greater than300,000, and return their distance to highway 'I170'.

    Answer: Run the following query:

    SELECT /*+ ordered */c.city,sdo_nn_distance (1) distance_in_miles

    FROM geod_interstates i,geod_cities c

    WHERE i.highway = 'I170'AND sdo_nn (c.location, i.geom,

    'sdo_batch_size=5 unit=mile', 1) = 'TRUE'AND pop90 > 300000AND rownum < 6

    ORDER by distance_in_miles;

    4) Find all city-and-county pairs that interact.

    Answer:Run the following query:

    SELECT /*+ ordered use_nl(a,b) use_nl(a,c)*/b.city, c.county

    FROM TABLE(SDO_JOIN('GEOD_COUNTIES', 'GEOM','GEOD_CITIES', 'LOCATION','MASK=ANYINTERACT')) a,

    geod_cities b,geod_counties c

    WHERE a.rowid1 = c.rowidAND a.rowid2 = b.rowid

    ORDER BY b.city;

  • 8/12/2019 xpp_A_doc

    62/142

    Practice 10-1: Using the SDO_WITHIN_DISTANCE, SDO_NN, andSDO_JOIN Operators (continued)

    Oracle Spatial 11g: Essentials A - 62

    ..

    5) Find all cities within 20 miles of all interstate highways.

    Answer:Run the following query:

    SELECT /*+ ordered */ a.city, b.highwayFROM TABLE(SDO_JOIN(

    'GEOD_INTERSTATES', 'GEOM','GEOD_CITIES', 'LOCATION','DISTANCE=20 UNIT=MILE')) c,

    geod_cities a,geod_interstates b

    WHERE c.rowid1 = b.rowidAND c.rowid2 = a.rowidORDER BY a.city;

    ..

  • 8/12/2019 xpp_A_doc

    63/142

    Oracle Spatial 11g: Essentials A - 63

    Practices for Lesson 11

    In this practice, you solve queries by using the area, length, and distance calculation

    functions such as SDO_AREA, SDO_LENGTH, and SDO_DISTANCE. You run queries toperform buffer operations. You also work on spatial Boolean queries.

    Prerequisite:To perform this practice completely, you must have completed thepractices for lessons 2, 3, 5, 7, and 8 successfully.

    If you did not, refer to the Prerequisitesection in the practice for lesson 9.

  • 8/12/2019 xpp_A_doc

    64/142

    Oracle Spatial 11g: Essentials A - 64

    Practice 11-1: Analyzing Geometries by Using Spatial Operatorsand Functions

    1) Find the sum of the area in square miles of all counties around Passaic County in New

    Jersey.

    Answer: Run the following query:

    SELECT /*+ ordered */ sum (sdo_geom.sdo_area (c1.geom, 0.5,'unit=sq_mile')) area

    FROM geod_counties c2,geod_counties c1

    WHERE c2.state = 'New Jersey'AND c2.county = 'Passaic'AND sdo_relate (c1.geom, c2.geom, 'mask=TOUCH

    querytype=WINDOW') = 'TRUE';

    2) Find the sum of the area in square miles of all counties around Passaic County in NewJersey, categorized by state.

    Answer:Run the following query:

    SELECT /*+ ordered */ c1.state,SUM (sdo_geom.sdo_area(c1.geom, 0.5, 'unit=sq_mile'))

    areaFROM geod_counties c2,

    geod_counties c1WHERE c2.state = 'New Jersey'

    AND c2.county = 'Passaic'AND SDO_RELATE (c1.geom, c2.geom, 'mask=TOUCH

    querytype=WINDOW') = 'TRUE'GROUP BY c1.state;

    3) Find all states with a population density over 500 per square mile, and display thearea of each state in square kilometers. Round off the population density.

    Answer: Run the following query:

    SELECT state,ROUND (poppsqmi) poppsqmi,sdo_geom.sdo_area (geom, 0.5, 'unit=sq_km') area

    FROM geod_statesWHERE poppsqmi > 500;

  • 8/12/2019 xpp_A_doc

    65/142

    Practice 11-1: Analyzing Geometries by Using Spatial Operatorsand Functions (continued)

    Oracle Spatial 11g: Essentials A - 65

    4) Find all states with borders longer than 1,500 miles, and return the length of each

    border.

    Answer: Run the following query:

    SELECT a.state, a.lengthFROM (SELECT state,

    sdo_geom.sdo_length (geom, 0.5, 'unit=mile') length FROMgeod_states) aWHERE a.length > 1500;

    5) Calculate the minimum distance between the highway I170 and Passaic County inmiles.

    Answer: Run the following query:

    SELECT SDO_GEOM.SDO_DISTANCE (a.geom, b.geom, 0.5,'unit=mile') distance

    FROM geod_interstates a,geod_counties b

    WHERE b.county = 'Passaic' and b.state_abrv = 'NJ'AND a.highway = 'I170';

  • 8/12/2019 xpp_A_doc

    66/142

    Practice 11-1: Analyzing Geometries by Using Spatial Operatorsand Functions (continued)

    Oracle Spatial 11g: Essentials A - 66

    6) Generate a thirty-kilometer buffer around Interstate I95.

    Answer: Run the following query:

    SELECT sdo_geom.sdo_buffer(geom, 30, 0.5, 'arc_tolerance=0.05 unit=km')

    buffer_geomFROM geod_interstatesWHERE highway = 'I95';

    7) Find all counties that have any interaction with a thirty-kilometer buffer aroundhighway I95.

    Answer: Run the following query:

    SELECT /*+ ordered */ c.countyFROM geod_interstates i,

    geod_counties cWHERE i.highway = 'I95'

    AND sdo_anyinteract (c.geom,sdo_geom.sdo_buffer (i.geom, 30, 0.5, 'ARC_TOLERANCE=0.05UNIT=KILOMETER')) = 'TRUE';

    ..

    8) In the geom_datatable, insert two optimized rectangles by using the followingvalues:

    i) Object_id:20

    ii) Name:Rectangle1

    iii) Coordinates:(5,5), (9,11)

    i) Object_id:21

    ii) Name:Rectangle2

    iii) Coordinates:(8,8), (12,14)

  • 8/12/2019 xpp_A_doc

    67/142

    Practice 11-1: Analyzing Geometries by Using Spatial Operatorsand Functions (continued)

    Oracle Spatial 11g: Essentials A - 67

    Answer:To insert two optimized rectangles, run the following INSERTstatements:INSERT INTO geom_data VALUES(20,'Rectangle1',SDO_GEOMETRY( 2003, NULL, NULL,

    SDO_ELEM_INFO_ARRAY(1,1003, 3),

    SDO_ORDINATE_ARRAY(5,5, 9,11)));

    INSERT INTO geom_data VALUES(21,'Rectangle2',SDO_GEOMETRY(2003, NULL, NULL,

    SDO_ELEM_INFO_ARRAY(1,1003, 3),SDO_ORDINATE_ARRAY(8,8, 12,14))

    );

    9) View the two inserted rectangles in MapViewer.

    Answer: To view the rectangles, run the following queries:

    a) In the query1 window, enter the following query:

    SELECT shape from geom_data where object_id=20

    b) In the query2 window, enter the following query:

    SELECT shape from geom_data where object_id=21

  • 8/12/2019 xpp_A_doc

    68/142

    Practice 11-1: Analyzing Geometries by Using Spatial Operatorsand Functions (continued)

    Oracle Spatial 11g: Essentials A - 68

    Note:Change the fill color for the first query to yellow and for the second query to red.Make sure that the Label column field for both query windows is empty.

    10) Now, perform a UNIONoperation on the two rectangles. The query should return thegeometry object that is a topological union of the two rectangles. View the result in

    MapViewer.Answer:Enter the following query in the query1: window:

    SELECT sdo_geom.sdo_union(a.shape, b.shape, 0.5)

    FROM geom_data a,geom_data b

    WHERE a.object_id = 20AND b.object_id = 21;

  • 8/12/2019 xpp_A_doc

    69/142

    Practice 11-1: Analyzing Geometries by Using Spatial Operatorsand Functions (continued)

    Oracle Spatial 11g: Essentials A - 69

  • 8/12/2019 xpp_A_doc

    70/142

    Oracle Spatial 11g: Essentials A - 70

    Practices for Lesson 12

    In this practice, you solve spatial queries by using the analysis, MBR, utility, and

    aggregate functions.

    Prerequisite:To perform this practice completely, you must have completed the

    practices for lessons 2, 3, 5, 7, and 8 successfully.If you did not, refer to the Prerequisitesection in the practice for lesson 9.

  • 8/12/2019 xpp_A_doc

    71/142

    Oracle Spatial 11g: Essentials A - 71

    Practice 12-1: Using Spatial Analysis, MBR, Utility, andAggregate Functions

    1) Write a query to find the centroid (or center of gravity) of the state of Texas.

    Answer:Run the following query:

    SELECT sdo_geom.sdo_centroid(c.geom, 0.5) centroidFROM geod_states cWHERE c.state = 'Texas';

    2) Write a query to generate the minimum bounding rectangle of the state of Texas.

    Answer:Run the following query:

    SELECT sdo_geom.sdo_mbr(c.geom) mbr

    FROM geod_states cWHERE c.state = 'Texas';

    3) Write a query to find the number of vertices and number of elements in the geometry

    for the state of Texas.

    Answer:Run the following query:

    SELECT sdo_util.getnumelem(a.geom),sdo_util.getnumvertices(a.geom)FROM geod_states aWHERE a.state='Texas';

    4) From the GEOD_STATES(GEOM)layer, generate a geometry that represents theunion of the following states: NH,VT, and NY. View the results in MapViewer.

    Answer:Perform the following steps:

    a) Enter the following query in query1: window. Set the fill color to yellow.

    SELECT sdo_aggr_union(sdoaggrtype(a.geom, 0.5)) cow_states

    FROM geod_states aWHERE a.state_abrv in ('NH', 'VT', 'NY')

  • 8/12/2019 xpp_A_doc

    72/142

    Practice 12-1: Using Spatial Analysis, MBR, Utility, andAggregate Functions (continued)

    Oracle Spatial 11g: Essentials A - 72

    b) Enter the following query in query2: window. Set the line color to red.

    SELECT geomFROM geod_counties aWHERE a.state_abrv in ('NH', 'VT', 'NY')

    5) From the GEOD_STATES(GEOM)layer, find the centroid of the following NewEngland states: MA, NH, VT, RI, CT, and ME. View the results in MapViewer.

    Answer:Perform the following steps:

    a) Enter the following query in the query1: window:

    SELECT sdo_aggr_centroid(sdoaggrtype(a.geom, 0.5))

    FROM geod_states aWHERE a.state_abrv in ('MA', 'NH', 'VT', 'CT', 'RI', 'ME')

    b) Enter the following query in the query2: window.

    SELECT geomFROM geod_counties aWHERE a.state_abrv in ('NH', 'MA', 'RI', 'VT', 'CT', 'ME')

  • 8/12/2019 xpp_A_doc

    73/142

    Practice 12-1: Using Spatial Analysis, MBR, Utility, andAggregate Functions (continued)

    Oracle Spatial 11g: Essentials A - 73

    6) From the GEOD_STATES(GEOM)layer, find the geometry that forms a convex hullaround the following New England states: MA, NH, VT, RI, CT, and ME. View theresults in MapViewer.

    Answer:Perform the following steps:

    a) Enter the following query in the query1: window:

    SELECT sdo_aggr_convexhull(sdoaggrtype(a.geom, 0.5))FROM geod_counties aWHERE a.state_abrv in ('MA', 'NH', 'VT', 'RI', 'CT', 'ME');

    b) Enter the following query in the query2: window. Set the line color to black and

    fill color to red.

    SELECT geomFROM geod_counties aWHERE a.state_abrv in ('NH', 'MA', 'RI', 'VT', 'CT', 'ME');

  • 8/12/2019 xpp_A_doc

    74/142

    Practice 12-1: Using Spatial Analysis, MBR, Utility, andAggregate Functions (continued)

    Oracle Spatial 11g: Essentials A - 74

  • 8/12/2019 xpp_A_doc

    75/142

    Oracle Spatial 11g: Essentials A - 75

    Practices for Lesson 13

    In this practice, you get a thorough hands-on practice with the Map Builder tool. In this

    practice, you create themes and then associate them with a base map. Then you use a

    mapclient JSP demo to publish the map to the Web. For this practice, you must access

    mapbuilder_10131.ziplocated in the D:\labs\softwarefolder.

    Prerequisite:To perform this practice completely, you must have completed the

    practices for lessons 2, 3, 5, 7, and 8 successfully.

    If you did not, refer to the Prerequisitesection in the practice for lesson 9.

  • 8/12/2019 xpp_A_doc

    76/142

    Oracle Spatial 11g: Essentials A - 76

    Practice 13-1: Defining Maps by Using the Map Builder Tool

    Part 1

    As part of this course, many styles are supplied in the Oracle Data Pump format in the

    D:\labs\data\styles.dmpfile.1) Assuming that you created a directory object, student_dumpdir, in the previous

    lesson practice, import the styles.dmpfile by using impdp.

    Answer: To import the styles.dmpfile, perform the following steps:

    a) Open a command window and enter the following:

    impdp student/student dumpfile=student_dumpdir:styles.dmpnologfile=y

    2) Move the contents of the styles table that you imported into USER_SDO_STYLES.Commit the records.

    Answer: Perform the following steps:

    a) Assuming that you have logged in as student in SQL*Plus, run the following

    INSERTstatement:

    INSERT INTO user_sdo_styles SELECT * FROM styles;COMMIT;

    3) For this practice, you must extract the mapbuilder_10131.zipfile into a newfolder under the root D drive:D:\. The mapbuilder_10131.zipfile is placedin the D:\labs\softwarefolder. Create a new directory, mapbuilder, underD:\and extract the zip file there.

    Answer:Perform the following steps:

    a) Create the mapbuilderdirectory under D:\.

  • 8/12/2019 xpp_A_doc

    77/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 77

    b) Double-click the mapbuilder_10131.zipfile to open WinZip. ClickExtract. Choose D:\mapbuilderas the location to extract the files into.

    4) Start MapBuilder and establish a database connection.

    Answer: Perform the following steps:

    a) Open a new command prompt, and change to the D:\mapbuilderfolder.

    b) To start Map Builder, run the following command:

    java -jar mapbuilder.jar

    c) The Oracle Map Builder interface opens, as shown in the following screenshot:

    d) To create a database connection, from the File menu, select New Connection. In

    the Add Connection dialog box, enter the following values:

    i) Connection Name: student

    ii) Host: localhost

    iii) Port: 1521

  • 8/12/2019 xpp_A_doc

    78/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 78

    iv) SID: orcl

    v) Driver: thin

    vi) User: student

    vii) Password: student

    e) Click OK.

    f) The Connection drop-down box shows the student connection, as shown in thefollowing screenshot:

  • 8/12/2019 xpp_A_doc

    79/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 79

    Part 2

    5) A theme is analogous to a geometry column. Start the Create Geometry Theme

    Wizard.

    i) Create a theme definition called STATESthat represents theGEOD_STATES(geom)layer.

    ii) Select the STUDENT:C.US MAP YELLOWrender style. Select theSTUDENT:T.STATE NAMEtext style for the label column, STATE_ABRV.

    iii) Preview the STATEStheme.

    Answer: Perform the following steps:

    a) In Map Builder, right-click Themes, and select Create Geometry Themeto

    display the Create Geometry Theme Wizard. Click Nexton the first page.

  • 8/12/2019 xpp_A_doc

    80/142

  • 8/12/2019 xpp_A_doc

    81/142

  • 8/12/2019 xpp_A_doc

    82/142

  • 8/12/2019 xpp_A_doc

    83/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 83

    g) Click Finishto exit the Create Geometry Theme Wizard.

    h) Expand Themes > Geometry Themes. Right-click STATESand select Preview.

    Press the green arrow ( ) to start previewing. You can zoom in or out to

    preview.

  • 8/12/2019 xpp_A_doc

    84/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 84

    6) Create another theme named COUNTIESthat represents the GEOD_COUNTIES(geom)layer.

    i) Select the STUDENT:C.BLACK GRAYrender style.

    ii) Select the STUDENT:T.STATE NAMEtext style for the label column,COUNTY.

    iii) Preview the COUNTIEStheme through Map Builder.

    Answer:Perform the following steps:

    a) In Map Builder, right-click Themesand select Create Geometry Themeto

    display the Create Geometry Theme Wizard. Click Nexton the first page.

    b) Create a theme definition called COUNTIESthat represents theGEOD_COUNTIES(geom)layer. Enter COUNTIESin the Name field and selectthe GEOD_COUNTIEStable from the Base Table drop-down list. Click Next.

    c) For Style Type set to Color, click Select. From the Style Picker dialog box, select

    the STUDENT:C.BLACK GRAYrender style. Click OK. Click Next.d) Enable Label Style. Select Text for Style Type. Click Select. In the Style Picker

    dialog box, select the STUDENT:T.STATE NAMEtext style for the labelcolumn. Click OK.

    e) In the Attribute drop-down list, select COUNTYas the label column. Set the LabelFunc to 1 to display the label column.

  • 8/12/2019 xpp_A_doc

    85/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 85

    f) Click Nextuntil you see the Finish button. View the XML code that Map Builder

    will insert into USER_SDO_GEOM_THEMESwhen you click Finish.

    g) Click Finishto exit the Create Geometry Theme Wizard.

    h) Expand Themes > Geometry Themes. Right-click COUNTIES and select

    Preview. Press the green arrow ( ) to start previewing. You can zoom in or outto preview.

  • 8/12/2019 xpp_A_doc

    86/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 86

    7) A map can be associated with one or more themes. Create a map definition called

    SAMPLE_MAP.

    i) Add the STATEStheme to SAMPLE_MAP. Set max_scaleto 12,000,000.

    ii) Add the COUNTIEStheme to SAMPLE_MAP. Set min_scaleto12,000,000.

    iii) Preview the SAMPLE_MAPmap through Map Builder.

    Answer: Perform the following steps:

    a) In Map Builder, right-click Base Maps andselectCreate Base Mapto start the

    Create Base Map Wizard. Click Nexton the first page.

    b) Enter SAMPLE_MAPin the name field. Click Next.

    c) Add the STATEStheme to SAMPLE_MAP. Also, add the COUNTIEStheme toSAMPLE_MAP. Select the theme from the Theme Names box and click the firstbutton (highlighted in the screenshot) to add the theme to SAMPLE_MAP.

    d) Make the STATEStheme render until you zoom in to a scale of 1:12,000,000, andthen turn it off (set max_scaleto 12,000,000).

    e) Make the COUNTIEStheme render any time that you zoom in more than a scaleof 1:12,000,000 (set min_scaleto 12,000,000).

  • 8/12/2019 xpp_A_doc

    87/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 87

    f) Click Nextto see the XML that Map Builder will insert into

    USER_SDO_GEOM_MAPSwhen you click Finish.

    g) Click Finishto exit the Create Base Map Wizard.

    h) To preview the SAMPLE_MAPmap through Map Builder, expand Base Maps.

    Right-click SAMPLE_MAPand select Preview.i) Click the green arrow to preview. You can zoom in or out to preview.

  • 8/12/2019 xpp_A_doc

    88/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 88

    8) Now you are ready to publish maps to the Web. MapViewer includes a JSP demo to

    help you get started. View the SAMPLE_MAPthat you created earlier.

    Answer:Perform the following steps:

    a) From the MapViewer page, click the Demostab. Click the mapclientdemo.

    b) Populate the following fields:

    i) MapViewer URL: http://localhost:8888/mapviewer/omserver

    ii) Data Source: student

    iii) Title: Sample Map(optional)

    iv) Base Map: sample_map

    v) Map Center X Coord: -70

    vi) Map Center Y Coord: 42

    vii) Map Size: 10

  • 8/12/2019 xpp_A_doc

    89/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 89

    Note:Prior to Ratio Scale, a different mechanism called Map Size was used. Map

    Size is no longer recommended, but the version of mapclient.jspthat shipswith MapViewer 10.1.3.1 still expects Map Size, and not Ratio Scale. Map Size is

    the distance from the north to south in the map display. In this example, it would

    be 10 degrees north to south.

    c) Click Submit. You can recenter it by clicking the new map center location. Try

    out the Zm In, Zm Out, Pan W., Pan N., Pan S., and Pan E. buttons.

    d) Click Zm Out until you see the complete US Map with state names.

  • 8/12/2019 xpp_A_doc

    90/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 90

    e) Now keep clicking ZM In.

  • 8/12/2019 xpp_A_doc

    91/142

    Practice 13-1: Defining Maps by Using the Map Builder Tool(continued)

    Oracle Spatial 11g: Essentials A - 91

    f) View the XML map request submitted by the JSP in the Request/Response/Msg

    window.

  • 8/12/2019 xpp_A_doc

    92/142

    Oracle Spatial 11g: Essentials A - 92

    Practices for Lesson 14

    In this practice, you set up and run Oracle Maps demos. For this practice, you use a zip

    file called mvdemo_10131.zipplaced in the D:\labs\softwarefolder.

    Prerequisite:To perform this practice completely, you must have completed the

    practices for lessons 7 and 13.

  • 8/12/2019 xpp_A_doc

    93/142

    Oracle Spatial 11g: Essentials A - 93

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API

    1) From Windows Explorer, extract mvdemo_10131.zipto the local disk (the rootdirectory on the D: drivethat is, D:\.). When you extract the file, it creates theD:\mvdemodirectory.

    Answer:Perform the following steps:

    a) From Windows Explorer, double-click mvdemo_10131.zipto start WinZipand extract it to the local disk (the root directory on the D: drive is D:\).

    b) Click Extract. Enter D:\in the Extract to field of the pop-up menu. Click Extract.When you extract the file, it creates theD:\mvdemodirectory.

    2) Connect using system/oracleas the username/password. Create an mvdemouser identified by mvdemo. Grant connect, resource, and create view privileges to themvdemouser.

    Answer:Perform the following:a) In SQL*Plus, connect using system/oracleas the username/password and

    run the following command:

    conn system/oracleGRANT connect, resource, create view TO mvdemo IDENTIFIED BYmvdemo;

    3) Import the mvdemo.dmpfile. It is located in the D:\mvdemofolder.

    Answer:To import the mvdemo.dmpfile, at the command prompt, enter the followingcommand:

    imp mvdemo/mvdemo file=d:\mvdemo\mvdemo.dmp full=y ignore=y

  • 8/12/2019 xpp_A_doc

    94/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 94

    4) Log in as the mvdemouser in SQL*Plus and run the mvdemo.sqlscript.

    Answer: Run the following:

    conn mvdemo/mvdemo@d:\mvdemo\mvdemo.sql

    5) Through the MapViewer admin page, configure a data source for the MapViewer

    demos. Name the data source MVDEMO. Create the data source in a way that

    automatically starts when MapViewer starts.

    Answer:Perform the following steps:

    a) Make sure that the MapViewer is running in the OC4J console. If not, double-

    click the start.batfile in the D:\mv10131_qsdirectory.

    b) Start Internet Explorer and enter the following URL:http://localhost:8888/mapviewer/

    c) On the upper-right corner of the MapViewer page, click Admin. Log in as the

    oc4jadminuser and enter welcomeas the password.

    d) Under the Management tab, select Configuration under Manage MapViewer.

    e) At the bottom of the configuration file, either uncomment the example

    map_data_sourceelement or if you modified it in Practice 7-1, copy andpaste the map_data_sourceelement and modify the following values:

    i) Name: mvdemo

    ii) Host: localhost

  • 8/12/2019 xpp_A_doc

    95/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 95

    iii) Port: 1521

    iv) Sid: orcl

    v) User: mvdemo

    vi) Password:!mvdemo

    vii) # Mappers: 3

    f) Click Save & Restart.Click the Datasources link under Manage MapViewer.

    The mvdemodata source should be listed under Existing data sources.6) Create the USER_SDO_CACHED_MAPSandALL_SDO_CACHED_MAPSmetadata

    views. This is a one-time task. Note that in a future release of Oracle Database, thesemetadata views will be created automatically when Oracle Locator or Oracle Spatial

    is installed.

    Answer:Perform the following steps:

    a) Log in using system/oracleas the username/password.

    conn system/oracle

    b) Run the following SQL script.

    @d:\mv10131_qs\oc4j\j2ee\home\applications\mapviewer\web\WEB-INF\admin\mcsdefinition.sql;

    7) From the MapViewer Admin page, create a cache map instance for the DEMO_MAPbase map, which was loaded when mvdemo.dmpwas imported.

  • 8/12/2019 xpp_A_doc

    96/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 96

    Answer:Perform the following steps:

    a) From the MapViewer Admin page, click the Management tab, and selectManage Map Caches and Create. The Select type of map source menu must

    be set to Internal.

    b) Click Continue. Enter the following values:

    i) Name: DEMO_MAP

    ii) Data source: mvdemo

    iii) Base map: DEMO_MAP

    iv) # Zoom Levels: 10

    v) Minimum map size: 1000

    vi) Maximum map size: 25000000

    vii) SRID: 8307viii) Min X: -180

    ix) Max X: 180

    x) Min Y: -90

    xi) Max Y: 90

  • 8/12/2019 xpp_A_doc

    97/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 97

    c) Click Submit.

    8) Now you can run some of the Oracle Maps demos. You can try some of the

    following:

    i) maps and faces

    ii) Display Map

    iii) Navigation Panel

    iv) Theme Based FOI layer visibility

    v) Red Lining

    vi) Scale Bar

    vii) Circle Tool

  • 8/12/2019 xpp_A_doc

    98/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 98

    viii) Collapsible Overview Map

    ix) Marquee Zoom

    x) Move FOI

    Answer:To run some of the Oracle Maps demos, perform the following steps:a) From the MapViewer Web page, click the Demostab.

    b) At the bottom of the page, click the Oracle Maps tutorial. Click Running thedemos.

    c) Each demo is interactive, and the source code is displayed if you scroll to thebottom.

  • 8/12/2019 xpp_A_doc

    99/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 99

    9) You can create a map cache instance for any base map defined in USER_SDO_MAPS.From the MapViewer Admin page, create a cache map instance for the

    SAMPLE_MAPbase map that you created in the lesson titled Defining Maps byUsing the Map Builder Tool.

    Answer: Perform the following:

    a) From the MapViewer Admin page, click the Management tab, and selectManage Map Caches and Create. The Select type of map source menu must

    be set to Internal. Click Continue.

    b) Enter the following values:

    i) Name: SAMPLE_MAP_CACHE

    ii) Data Source: student

  • 8/12/2019 xpp_A_doc

    100/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 100

    iii) Base map: SAMPLE_MAP

    iv) # Zoom Levels: 10

    v) Minimum map size: 1000

    vi) Maximum map size: 25000000

    vii) SRID: 8307

    viii) Min X: -180

    ix) Max X: 180

    x) Min Y: -90

    xi) Max Y: 90

    c) Click submit.

    10) Oracle MapViewer ships with a demo to test map cache instances that you create. The

    demo is called OMaps. Test the SAMPLE_MAP_CACHEmap cache instance byrunning the OMaps demo.

    Answer: Perform the following steps:

    a) From the MapViewer Web page, click the Demostab.

    b) Click the OMapsdemo.

    c) Enter the following values:

    i) Datasource: student

    ii) Cached Base Map: sample_map_cache

    iii) Center_x: -122.4

    iv) Center_y: 37.8

    v) Initial Zoom Level: 4

    vi) Map width: 800

    vii) Map height: 600

  • 8/12/2019 xpp_A_doc

    101/142

    Practice 14-1: Leveraging Oracle Maps: The Map Cache andJavaScript API (continued)

    Oracle Spatial 11g: Essentials A - 101

    d) Click Proceed. Now, you can try out the demo. Click the controls on the map

    (highlighted by the red rectangle in the screenshot) to zoom in or out or move to a

    particular location on the map.

  • 8/12/2019 xpp_A_doc

    102/142

    Oracle Spatial 11g: Essentials A - 102

    Practices for Lesson 15

  • 8/12/2019 xpp_A_doc

    103/142

    Oracle Spatial 11g: Essentials A - 103

    Practice 15-1: Creating a User-Defined Coordinate System

    No practice in this lesson.

  • 8/12/2019 xpp_A_doc

    104/142

    Oracle Spatial 11g: Essentials A - 104

    Practices for Lesson 16

    In this practice, you linear-reference geometries and use some SDO_LRSpackagefunctions. You also run dynamic segmentation queries.

    Prerequisite:To perform this practice completely, you must have completed the

    practices for lessons 2, 3, 5, 7, and 8 successfully.

    If you did not, refer to the Prerequisitesection in the practice for lesson 9.

  • 8/12/2019 xpp_A_doc

    105/142

    Oracle Spatial 11g: Essentials A - 105

    Practice 16-1: Implementing a Linear Referencing System

    In this practice, it is assumed that you are connected as student.

    1) Using data from the GEOD_INTERSTATEStable, create an LRS_HIGHWAYStable.The following is the structure of the LRS_HIGHWAYStable:

    i) Highway varchar2(35)

    ii) Geom. sdo_geometry

    Answer:To create the LRS_HIGHWAYStable, run the following statement:

    CREATE TABLE lrs_highways (highway VARCHAR2(35),geom SDO_GEOMETRY);

    2) The LRS_HIGHWAYStable contains highways that interact with the state ofDelaware (DE), but are clipped at the DE border. Write a query to clip highways at

    the DE border, and insert the clipped geometries into the LRS_HIGHWAYStable.

    Answer: Run the following query:

    INSERT INTO lrs_highwaysSELECT /*+ ordered */

    b.highway,sdo_geom.sdo_intersection(a.geom, b.geom, 0.5) geom

    FROM geod_states a,geod_interstates b

    WHERE a.state_abrv = 'DE'AND SDO_ANYINTERACT(b.geom, a.geom) = 'TRUE';

    3) Insert 2D metadata for the new layer you created, LRS_HIGHWAYS(geom), into theUSER_SDO_GEOM_METADATAview.

    Answer: To insert metadata, run the following query:

    INSERT INTO user_sdo_geom_metadataVALUES (

    'LRS_HIGHWAYS', 'GEOM',sdo_dim_array (sdo_dim_element ('Long', -180, 180, 0.5),sdo_dim_element ('Lat', -90, 90, 0.5)),

    8307);

  • 8/12/2019 xpp_A_doc

    106/142

    Practice 16-1: Implementing a Linear Referencing System(continued)

    Oracle Spatial 11g: Essentials A - 106

    4) Linear-reference all geometries in the LRS_HIGHWAYS(geom)layer. Also,confirm that the metadata was updated.

    Answer:Execute the following PL/SQL block of code:

    set serveroutput on;

    declarestatus varchar2(32);

    beginstatus := SDO_LRS.CONVERT_TO_LRS_LAYER('LRS_HIGHWAYS','GEOM',

    0, 1000000, 0.5);dbms_output.put_line('Status = '||status);

    end;

    SELECT *FROM user_sdo_geom_metadataWHERE table_name = 'LRS_HIGHWAYS'AND column_name = 'GEOM';

    Note:The geometries in LRS_HIGHWAYSwere linear-referenced withSDO_LRS.CONVERT_TO_LRS_LAYER. Each geometrys start measure was set to 0,and each geometrys end measure was set to the geometry length. For geodetic

    geometries, the length is in meters. For nongeodetic geometries, the length is in the

    coordinate system unit.

    5) Create an index on the LRS_HIGHWAYS(highway)column.

    Answer: To create an index, run the following query:

    CREATE INDEX lrs_highway_idx ON LRS_HIGHWAYS (highway);

    6) Find the length of each highway in DE. Specify the length in meters.Answer: To find the length of each highway in DE, run the following query:

    SELECT highway,SDO_LRS.GEOM_SEGMENT_END_MEASURE(geom)

    "LENGTH_IN_METERS"FROM lrs_highways;

  • 8/12/2019 xpp_A_doc

    107/142

    Practice 16-1: Implementing a Linear Referencing System(continued)

    Oracle Spatial 11g: Essentials A - 107

    7) Create a table called PAVEMENT_CONDITIONfor storing the pavement conditionsassociated with highway segments in DE. The PAVEMENT_CONDITIONtable mustnot contain geometries, but only the measure values associated with the linear-

    referenced geometries in LRS_HIGHWAYS. The following is the structure of the

    table:i) highway varchar2(35)

    ii) from_measure number

    iii) to_measure number

    iv) condition varchar2(6)

    Answer: To create thePAVEMENT_CONDITIONtable, run the following query:CREATE TABLE pavement_condition (

    highway varchar2(35),from_measure number,

    to_measure number,condition varchar2(6));

    8) Insert records with highway-segment pavement conditions into the

    PAVEMENT_CONDITIONtable. Use the following values:

    i) 'I95',0,5951.5,'good'

    ii) 'I95',5951.5, 28000,'fair'

    iii) 'I95',28000, 37194, 'poor'

    iv) 'I495', 0, 16001, 'good'

    v) 'I495', 16001, 18004, 'poor'vi) 'I295', 0, 3000, 'poor'

    vii)'I295', 3000, 4000, 'fair'

    viii) 'I295', 4000, 8001.5, 'poor'

    ix) 'I295', 8001.5, 9603, 'good'

    Answer: To insert the records, run the following INSERTstatements (Use thesol_16_08.sqlscript located in d:\labs\solnfolder):

    insert into pavement_condition values ('I95', 0, 5951.5, 'good');

    insert into pavement_condition values ('I95',5951.5 , 28000, 'fair');

    insert into pavement_condition values ('I95', 28000, 37194, 'poor');

    insert into pavement_condition values ('I495', 0, 16001, 'good');

    insert into pavement_condition values (

  • 8/12/2019 xpp_A_doc

    108/142

    Practice 16-1: Implementing a Linear Referencing System(continued)

    Oracle Spatial 11g: Essentials A - 108

    'I495', 16001, 18004, 'poor');insert into pavement_condition values (

    'I295', 0, 3000, 'poor');insert into pavement_condition values (

    'I295', 3000, 4000, 'fair');

    insert into pavement_condition values ('I295', 4000, 8001.5, 'poor');

    insert into pavement_condition values ('I295', 8001.5, 9603, 'good');

    commit;

    9) Create an index on the PAVEMENT_CONDITION(highway)column.

    Answer:To create an index, run the following statement:

    CREATE INDEX pavement_condition_idx on pavement_condition(highway);

    10) Dynamically generate the clipped sections of highways in DE that have poor

    pavement condition. This operation is also known as dynamic segmentation. Use thePAVEMENT_CONDITIONtable to clip and return the highway segments where thepavement condition is poor.

    Hint:Join the PAVEMENT_CONDITIONand LRS_HIGHWAYStables.

    Answer: Run the following query to perform dynamic segmentation:

    SELECT sdo_lrs.clip_geom_segment(a.geom, b.from_measure, b.to_measure), a.highway

    FROM lrs_highways a,pavement_condition b

    WHERE b.condition = 'poor'AND a.highway = b.highway;

    11) From the beginning of the I95 highway, traverse 3,000 meters and return thelongitude and latitude at that location.

    Answer:Run the following query:

    SELECT sdo_lrs.locate_pt(a.geom, 3000, 0)FROM lrs_highways aWHERE highway = 'I95';

  • 8/12/2019 xpp_A_doc

    109/142

    Practice 16-1: Implementing a Linear Referencing System(continued)

    Oracle Spatial 11g: Essentials A - 109

    12) Find the measure position along highway I295 nearest to the followin