Spatial Database Tips and Tricks Presentation

Embed Size (px)

Citation preview

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    1/118

    2009.foss4g.org

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    2/118

    Spatial Database

     Tips & Tricks

    Paul Ramsey

    [email protected]

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    3/118

    Housekeeping

    • Copy workshop rom D!D

    • Downloa" rom

    http#$$%%.%%.%%.%%$%%%$sp"b''(.)ip

    • *nstall or not

    • *gnore me or not

    • +%amples also athttp#$$%%.%%.%%.%%#,','$spatial"btips

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    4/118

    *mpatient People

    • They try to install without rea"inginstructions

    • -hen you see an error bo% "uringPost*S install/ click 0Ignore1

    • Remember to create 0me"or"1

    2not 0postgis13 "atabase "uringPost*S install

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    5/118

    *mpatient People

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    6/118

    It’s not dead, it’s just resting…It’s not dead, it’s just resting…

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    7/118

    4oti5ation

    • Spatial "atabases are powerul

    • o"like/ really

    • 6ou "o not nee" 0*S sotware1

    • 6our "atabase is 0*S sotware1

    • 6ou "o not nee" 0spatialmi""leware1

    • See abo5e

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    8/118

    Stan"ar" Database

    • Has data types7 5archar

    7 integer

    7 real

    7 "ate

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    9/118

    Spatial Database

    • Has spatial data types7 point

    7 linestring

    7 polygon

    7 multipoint

    7 multilinestring7 multipolygon

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    10/118

    Stan"ar" Database

    • Has one-dimensional indexes7 btree

    7 hash

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    11/118

    Spatial Database

    • Has spatial indexes7 rtree

    7 8ua"tree

    7 gri"

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    12/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    13/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    14/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    15/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    16/118

    9in" intersecting shapes:

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    17/118

    Start with all bo%es/

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    18/118

    ;n" intersecting bo%es/

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    19/118

    then ;n" intersecting

    shapes.

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    20/118

    Stan"ar" Database

    • Has functions

    • -ork against stan"ar" types7 lower23

    7 roun"23

    7 substring23

    7 trim23

    7 "ayoweek 23

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    21/118

    Spatial Database

    • Has spatial functions

    • -ork against spatial  types

    7 ST

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    22/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    23/118

    Open Geospatial Consortium (OGC)

    Simple Features for SQL (SFSQL)

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    24/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    25/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    26/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    27/118

    Locator 

    Spatial

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    28/118

    •No buffer operation

    •No union operation•No intersection operation

    •No centroid point

    •No area or length calculation

    L O C A T O R

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    29/118

    • ?inear reerencing system 2?RS3 support

    • Spatial analysis an" mining unctions

    an" proce"ures 2SD

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    30/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    31/118

    • S9SB? compliant

    • Aew release/ not as many eatures• Ao coor"inate reerence system

    transorms

    • -in"ows only• ri" in"e%

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    32/118

    SELECT * FROM the_table

    WHERE the_geom.STIntersects(

      geometry::STGeomFromText('POINT(0 0)',0)  );

    SELECT * FROM the_table

    WHERE ST_Intersects(  the_geom,

      ST_GeomFromText('POINT(0 0)',0)

      );

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    33/118

    • PostgreSB? $ Post*S

    • S9SB? compliant

    • pen source 2P?3

    • Proprietary $ open source clients

    •0geographic1 coor"inates re8uirecare

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    34/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    35/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    36/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    37/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    38/118

    • ST

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    39/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    40/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    41/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    42/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    43/118

    *nstallation

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    44/118

    *nstallation

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    45/118

    *nstallation

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    46/118

    *nstallation

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    47/118

    Data

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    48/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    49/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    50/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    51/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    52/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    53/118

     Tomcat

    • IAD* con;gure"o PostgreSB? IDJC

    o

    Connection to 0me"or"1 "atabase  IS?T installe"

    o Kc#L Ks8l#L

    eoSer5er installe"

    o Connections to 0me"or"1 tableso Styles or 0me"or"1 tables

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    54/118

     Tomcat

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    55/118

    Workshop

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    56/118

    M' Jase 4ap with -4S

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    57/118

    !

    "

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    58/118

    !

    "

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    59/118

    #ttp$%%local#ost$&'&'%geoserer%

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    60/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    61/118

     

    var lon = -122.8450;

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    62/118

    var lat = 42.3438;

    var zoom = 18;

    var map;

    function initMap() {  map = new OpenLayers.Map( 'map' ,

      {controls:[new OpenLayers.Control.MouseDefaults(),

      new OpenLayers.Control.LayerSwitcher(),

      new OpenLayers.Control.PanZoomBar()],

      numZoomLevels:20});

      var gmap = new OpenLayers.Layer.Google(

      "Google Streets" // the default

      );

      var gsat = new OpenLayers.Layer.Google(

      "Google Satellite",

      {type: G_SATELLITE_MAP}  );

      map.addLayers([gmap, gsat]);

      map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);

    }

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    63/118

    // Initialize WMS layer from our local

    // GeoServer var bwms = new OpenLayers.Layer.WMS(

    "Medford Buildings",

      "http://localhost:8080/geoserver/wms?",

      { "transparent":"true",

      "layers":"medford:buildings",  "format":"image/png" },

      { "reproject":"true" }

      );

    // Add WMS layer to our map

    map.addLayer(bwms);

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    64/118

    M

    E7Clic k

    to

    Bu

    e

    ry

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    65/118

    ME 7 Click to Buery

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    66/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    67/118

    function queryDatabase(e) {

      // Read the map coordinates from the click event

      var lonlat = map.getLonLatFromViewPortPx(e.xy);

      // Read the table we are going to query from page

      var table = document.getElementById("table").value;

      // Construct the query URL   var url = "01-click-query.jsp";

      url += "?lon=" + lonlat.lon;

      url += "&lat=" + lonlat.lat;

      url += "&table=" + table;

      // Load the URL into an iframe

      document.getElementById("query").src = url;

    }

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    68/118

    01-click-query.jsp?

    lon=-122.8451943397522&

    lat=42.344141057680226&

    table=medford.taxlots

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    69/118

    . . . . . . .

    'Eclick8uery.Gsp

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    70/118

    select

      st_geometrytype(the_geom) as geometrytype,

      st_area(the_geom) as area,  *

    from ${param.table}

    where

      st_contains(

      the_geom,

      st_transform(

      st_setsrid(

      st_makepoint(${param.lon},${param.lat}),

      4326),  2270))

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    71/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    72/118

    SR*DFNF

    • ?ocation O Coor"inate SR*D

    • Here O 2EE.Q/ (.(3 +PS#(

    • ST

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    73/118

    MH7

    Clic

    kt

    o

    =n

    aly)e

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    74/118

    02-click-analyze.jsp?

    lon=-122.8451943397522&

    lat=42.344141057680226&

    radius=200

    select

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    75/118

    select

      count(*) as "Number of Lots",

      round(avg(st_area(the_geom))::numeric/43560, 1) || ' acres' as

    "Average Lot Area",

      '$' || avg(impvalue)::integer as "Average Improvement Value",  '$' || avg(landvalue)::integer as "Average Land Value",

      '$' || avg(impvalue + landvalue)::integer as "Average Total

    Value",

      avg(yearblt)::integer as "Average Year Built"

    from medford.taxlots

    where  st_dwithin(

      taxlots.the_geom,

      st_transform(

      st_setsrid(

      st_makepoint(${param.lon},${param.lat}),

      4326),  2270),

      ${param.radius}

      )

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    76/118

    • *n"e%e" "istance 8uery

    •ST

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    77/118

    M(

    7 Clic

    k an

    "

    Ioin

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    78/118

    oning ta*lots

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    79/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    80/118

    +ert#ing is related

    to eert#ing else,-ut near t#ings are

    more related t#andistant t#ings.

      - Waldo Tobler

    S f

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    81/118

    Spatial relations#ips are a uniersal /e for joining

    ot#er0ise disparate data.

    customer ta-le

    census ta-le

    road ta-le

    stream ta-le

    oning ta-le

    ta*lot ta-le

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    82/118

    select

      count(*)

    as num_lots,  sum(st_area(taxlot.the_geom))

    as total_lot_area,

      zone.zoning as zoning,

      sum(taxlot.landvalue)

    as total_land_value,

      sum(taxlot.landvalue) /

    sum(st_area(taxlot.the_geom))as value_per_ft

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    83/118

    from

      medford.taxlots taxlotjoin

    medford.zoning zone

      on (

      st_contains(

      zone.the_geom,

    st_centroid(taxlot.the_geom)

      )

      )

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    84/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    85/118

    where

    st dwithin(

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    86/118

      st_dwithin(

      taxlot.the_geom,

      st_transform(

      st_setsrid(

      st_makepoint(

      ${param.lon},

      ${param.lat}),  4326),

      2270),

      ${param.radius}

      )group by zone.zoning

    order by total_lot_area desc

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    87/118

    M

    7C

    lic kan

    "

    >n

    ion

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    88/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    89/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    90/118

    {"type":"MultiLineString",

     "coordinates":[[

      [4289753.869,253537.254],  [4290375.489,253518.361]

      ]]}

    eoISA eometry

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    91/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    92/118

    Feature

    Feature

    Geometr

    Geometr 1roperties

    1roperties

    F      

     e     a     t       u    r      e    

     C       o    

     l      l       e     c    

     t      i       o     n    

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    93/118

    ${param.sql}

    {"type":"FeatureCollection",

     "features":[

     {"type":"Feature",

      "geometry":,  "properties":{

     

     

      "":

      ""

      ,

       

    }}

     ,

    ]}

    l t t j (

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    94/118

    select st_asgeojson(

      st_transform(the_geom,900913)

      )

    from medford.taxlots

    where

      st_dwithin(

      the_geom,

      st_transform(  st_setsrid(

      st_makepoint(-13676108, 5212594),

      900913),

      2270),  100

      )

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    95/118

    // Make a fresh vector layer, pulling features our URL 

    json_layer = new OpenLayers.Layer.Vector("GeoJSON", {

      strategies: [new OpenLayers.Strategy.Fixed()],

      protocol: new OpenLayers.Protocol.HTTP({

      url: json_url,

      format: new OpenLayers.Format.GeoJSON()  })

      });

    // Add our vector layer to the map

    map.addLayer(json_layer);

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    96/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    97/118

    M

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    98/118

    MX

    7 =rbitr a

    ry

    SB?

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    99/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    100/118

    MT7

    -a lk

    a

    Ae

    twork

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    101/118

    me"or".storm

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    102/118

    me"or".storm

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    103/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    104/118

    select d.node_fm, d.node_to, d.pipe_idfrom

     medford.storm_drains d,

     medford.storm_drains e

    where st_dwithin(d.the_geom, e.the_geom, 5)

     and e.node_to = 'D371W28CN0134'

     and e.gid != d.gid

     and st_distance(

      st_endpoint(st_geometryn(e.the_geom, 1)),

      st_startpoint(st_geometryn(d.the_geom, 1))  )

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    105/118

    e.node_to = 'D371W28CN0134'

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    106/118

    ST

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    107/118

    ST

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    108/118

    e.gi" NO ".gi"

    st

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    109/118

    st

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    110/118

    st

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    111/118

    ST?T*?*A+STR*A22' '/ E E/ 33

    • To7 ?*A+STR*A2' '/ E E/ 3

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    112/118

    *n conclusion:

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    113/118

    Stan"ar"s are goo":

    SFSQL

    G3LGeo4SO5

    63L 73S

    768

    7697FS

    Special mi""leware is

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    114/118

    Special mi""leware is

    unnecessary:

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    115/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    116/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    117/118

  • 8/19/2019 Spatial Database Tips and Tricks Presentation

    118/118

    2009.foss4g.org