Querying data using spatial functions

更新时间:
复制 MD 格式

In Hologres, you can use PostGIS spatial functions to query spatial data in tables. This topic describes how to import data from an on-premises machine to Hologres and use PostGIS spatial functions to query data in Hologres.

Prerequisites

Background information

PostGIS is a spatial extension of PostgreSQL and provides spatial features including objects, indexes, functions, and operators.

This topic provides sample data tables that contain various types of spatial information, such as longitude, latitude, coordinates, and distance. You will use HoloWeb to create tables, import local data into the accommodations and zipcode tables, and then use PostGIS spatial functions to query the data.

Procedure

Step

Description

Step 1: Create Hologres tables

In a database in your Hologres instance, create the accommodations table to store accommodation data such as geographical locations (longitude and latitude) and listing names. Create the zipcode table to store Berlin postal code data.

Step 2: Import the sample spatial data

Use HoloWeb to import local data files into the accommodations and zipcode tables.

Step 3: Use spatial functions to query data

Use PostGIS spatial functions to query the spatial data in the two Hologres tables.

Step 1: Create Hologres tables

Create the accommodations and zipcode tables in your database.

  1. Log on to the HoloWeb SQL Editor.

  2. Click the image icon below the SQL Editor tab to open the Ad-hoc Query window. In the toolbar of the query window, select your Hologres Instance Name and Database.

  3. Enable the PostGIS extension.

    In the SQL editor, enter the following statement and click Run.

    CREATE extension IF NOT EXISTS postgis; -- Enable the PostGIS extension.
  4. Create the accommodations table.

    Run the following SQL statement to create the accommodations table. This table stores accommodation data, such as geographical locations (longitude and latitude), listing names, and other details.

    Note

    After the table is created, you can verify the result. In the Table Directory pane on the left, click the image refresh icon, and then navigate to public > Table. Alternatively, you can check the Operational Logs to confirm that the table was created successfully.

    CREATE TABLE public.accommodations (
      id INTEGER PRIMARY KEY,
      shape GEOMETRY,
      name VARCHAR(100),
      host_name VARCHAR(100),
      neighbourhood_group VARCHAR(100),
      neighbourhood VARCHAR(100),
      room_type VARCHAR(100),
      price SMALLINT,
      minimum_nights SMALLINT,
      number_of_reviews SMALLINT,
      last_review DATE,
      reviews_per_month NUMERIC(8,2),
      calculated_host_listings_count SMALLINT, 
      availability_365 SMALLINT
    );
  5. Create the zipcode table.

    Run the following SQL statement to create the zipcode table to store Berlin postal code data.

    Note

    After the table is created, you can verify the result. In the Table Directory pane on the left, click the image refresh icon, and then navigate to public > Table. Alternatively, you can check the Operational Logs to confirm that the table was created successfully.

    CREATE TABLE public.zipcode (
      ogc_field INTEGER PRIMARY KEY NOT NULL,
      wkb_geometry GEOMETRY,
      gml_id VARCHAR(256),
      spatial_name VARCHAR(256),
      spatial_alias VARCHAR(256),
      spatial_type VARCHAR(256)
     );

Step 2: Import the sample spatial data

Use the Import On-premises File feature in HoloWeb to import data into the accommodations and zipcode tables.

  1. In the HoloWeb console, click Data Solutions in the top navigation bar.

  2. On the Data Solutions page, click Import On-premises File in the navigation pane on the left, and then click New Data Import on the right.

  3. Specify the Hologres table to which you want to import data.

    In the Import On-premises File dialog box, enter a job name and select your instance, database, and table (either accommodations or zipcode). Then click Next.

  4. Specify the data that you need to import and the encoding format.

    On the Upload File tab, configure the following parameters and click Next.

    Parameter

    Description

    Select File

    Click Browse to upload the data file to import. Only files with the .txt, .csv, or .log extension are supported. Select the accommodations or zipcode data file that you downloaded.

    Delimiter

    The delimiter that is used to separate data entries. In this example, select SEMICOLON.

    Note

    You can also select the option to the right of the drop-down list and specify a custom delimiter based on your business requirements.

    Original Encoding

    For this example, select UTF-8.

    First Line as Header

    By default, this option is not selected. If the first row of the data that you want to import is used as the header of the Hologres table, select this option.

  5. Confirm the configurations.

    On the Import Overview tab, review the import information on the Upload page. This page displays a summary including the job name, target database, schema, and table. After confirming the information is correct, click Execute to start the import.

  6. Verify the execution result.

    After the job is complete, the system shows whether the execution is successful in the Import Overview step. If the execution fails, you can view the error cause for troubleshooting and import the data again.

    You can also execute SQL statements in the SQL editor to query the number of data entries or detailed data in the destination Hologres table:

    • Check the number of data entries

      The accommodations table contains 22,248 rows, and the zipcode table contains 190 rows.

      SELECT COUNT(*) FROM accommodations; -- Query the number of rows in the accommodations table.
      SELECT COUNT(*) FROM zipcode; -- Query the number of rows in the zipcode table.
    • Query the detailed data

      SELECT * FROM accommodations; -- Query the rows in the accommodations table.
      SELECT * FROM zipcode; -- Query the rows in the zipcode table.

Step 3: Use spatial functions to query data

After the required Hologres tables are created and the sample spatial data is imported to the tables, you can use spatial functions to query the spatial data in Hologres. The following examples are for your reference. For information about the syntax of spatial functions, see PostGIS for geographic information analysis.

  • Query the number of rows in the accommodations table where the Spatial Reference System Identifier (SRID) is 4326.

    • Sample code:

      SELECT COUNT(*) FROM public.accommodations WHERE ST_SRID(shape) = 4326;
    • Returned result:

       count 
      -------
       22248
      (1 row)
  • Use the well-known text (WKT) format to query geometry objects that meet the specified conditions. In this example, you can check whether the ZIP codes in the zipcodes Hologres table are stored in World Geodetic System 1984 (WGS84). The system uses an SRID of 4326.

    Note

    Only the spatial data entries that are in the same spatial reference system can be referenced by each other.

    • Sample code:

      SELECT  ogc_field
              ,spatial_name
              ,spatial_type
              ,ST_SRID(wkb_geometry)
              ,ST_AsText(wkb_geometry)
      FROM    public.zipcode
      ORDER BY spatial_name
      ;
    • Returned result:

      ogc_field  spatial_name  spatial_type  st_srid  st_astext
      ---------------------------------------------------------------
      0           10115        Polygon        4326     POLYGON((...))
      4           10117        Polygon        4326     POLYGON((...))
      8           10119        Polygon        4326     POLYGON((...))
      ...
      (190 rows returned)
  • Use the GeoJSON format to query the surface, the surface size, and the number of points on the surface for Mitte in Berlin with the SRID set to 10117.

    • Sample code:

      SELECT  ogc_field
              ,spatial_name
              ,spatial_type
              ,ST_AsGeoJSON(wkb_geometry)
              ,ST_Dimension(wkb_geometry)
              ,ST_NPoints(wkb_geometry)
      FROM    public.zipcode
      WHERE   spatial_name = '10117'
      ;
    • Returned result:

      ogc_field  spatial_name  spatial_type                                   st_dimension  st_npoint
      -----------------------------------------------------------------------------------------------
      4           10117         {"type":"Polygon", "coordinates":[[[...]]]}    2             331
  • Query the number of accommodations within 500 meters of the Brandenburg Gate with the SRID set to 4326.

    • Sample code:

      SELECT  COUNT(*)
      FROM    public.accommodations
      WHERE   ST_DistanceSphere(shape, ST_GeomFromText('POINT(13.377704 52.516431)', 4326)) < 500
      ;
    • Returned result:

       count 
      -------
          29
      (1 row)
  • Perform a rough estimate of the location of the Brandenburg Gate based on the information about nearby accommodations.

    • Sample code:

      WITH
          poi(loc) AS ( 
              SELECT st_astext(shape) 
              FROM accommodations 
              WHERE name LIKE '%brandenburg gate%' )
      SELECT  COUNT(*)
      FROM    accommodations a
              ,poi p
      WHERE   ST_DistanceSphere(a.shape, ST_GeomFromText(p.loc, 4326)) < 500
      ;
    • Returned result:

       count 
      -------
          60
      (1 row)
  • Query the detailed information about all the accommodations around the Brandenburg Gate and sort the accommodations in descending order by price.

    • Sample code:

      SELECT  name
              ,price
              ,ST_AsText(shape)
      FROM    public.accommodations
      WHERE   ST_DistanceSphere(shape, ST_GeomFromText('POINT(13.377704 52.516431)', 4326)) < 500
      ORDER BY price DESC
      ;
    • Returned result:

                              name                        | price |                st_astext                 
      ----------------------------------------------------+-------+------------------------------------------
       DUPLEX APARTMENT/PENTHOUSE in 5* LOCATION! 7583    |   300 | POINT(13.3826510209548 52.5159819722552)
       DUPLEX-PENTHOUSE IN FIRST LOCATION! 7582           |   300 | POINT(13.3799997083855 52.5135918444834)
       Luxury Apartment in Berlin Mitte with View         |   259 | POINT(13.3835653528534 52.516360156825)
       BIG APT 4 BLNCTY-CNTR 43-H6                        |   240 | POINT(13.3800222998777 52.5134224506894)
       BIG APARTMENT-PRIME LOCATION-BEST PRICE! B0303     |   240 | POINT(13.379745196599 52.5162648947249)
       BIG APARTMENT IN BRILLIANT LOCATION-CTY CENTRE B53 |   240 | POINT(13.381383105167 52.5157082721072)
       SONYCENTER: lux apartment - 3room/2bath. WIFI      |   235 | POINT(13.3743158954191 52.5125308432819)
       CENTRE APARTMENT FOR 6 | 8853                      |   220 | POINT(13.3819039478615 52.5134866767369)
       BIG APARTMENT FOR 6 - BEST LOCATION 8863           |   209 | POINT(13.3830430841658 52.5147824286783)
       3 ROOMS ONE AMAZING EXPERIENCE! 8762               |   190 | POINT(13.3819898503053 52.5144190764637)
       AAA LOCATION IN THE CENTRE H681                    |   170 | POINT(13.3821787206534 52.5129769242004)
       H672 Nice Apartment in CENTRAL LOCATION!           |   170 | POINT(13.3803137710339 52.5132386929089)
       "Best View -best location!"                        |   170 | POINT(13.3799551247135 52.5147888483851)
       H652 Best Location for 4!                          |   170 | POINT(13.3805705422409 52.5143845784482)
       H651 FIT´s for Four in a 5* Location!              |   150 | POINT(13.3822063502184 52.5134994650996)
       NEXT TO ATTRACTIONS! H252                          |   110 | POINT(13.3823616629115 52.5136258446666)
       CTY Centre Students Home| G4                       |   101 | POINT(13.3808081476226 52.5130957830586)
       Room for two with private shower / WC              |    99 | POINT(13.3786877948382 52.5208018292043)
       StudentsHome CityCentre Mitte 91-0703              |    95 | POINT(13.3810390515141 52.5142363781923)
       FIRST LOCATION - FAIR PRICE K621                   |    80 | POINT(13.3823909855061 52.5131554670458)
       LONG STAY FOR EXPATS/STUDENTS- CITY CENTRE | K921  |    75 | POINT(13.380320945399 52.512364557598)
       Nice4Students! City Centre 8732                    |    68 | POINT(13.3810147526683 52.5136623602892)
       Comfy Room in the heart of Berlin                  |    59 | POINT(13.3813167311819 52.5127345388756)
       FO(U)R STUDENTS HOME-Best centre Location!         |    57 | POINT(13.380850032042 52.5131726958513)
       Berlin Center Brandenburg Gate !!!                 |    55 | POINT(13.3849641540689 52.5163902851474)
       !!! BERLIN CENTER BRANDENBURG GATE                 |    55 | POINT(13.379997730927 52.5127577639174)
       Superb Double Bedroom in Central Berlin            |    52 | POINT(13.3792991992688 52.5156572293422)
       OMG! That’s so Berlin!                            |    49 | POINT(13.3754883007165 52.5153487677272)
       Apartment in Berlin's old city center              |    49 | POINT(13.3821761577766 52.514037240604)
      (29 rows)
  • Query the detailed information about the accommodation with the highest price and its ZIP code.

    • Sample code:

      SELECT  a.price
              ,a.name
              ,ST_AsText(a.shape)
              ,z.spatial_name
              ,ST_AsText(z.wkb_geometry)
      FROM    accommodations a
              ,zipcode z
      WHERE   price = 9000
      AND     ST_Within(a.shape, z.wkb_geometry)
      ;
    • Returned result:

      price   name                                 st_astext                                  spatial_name      st_astext
      -------------------------------------------------------------------------------------------------------------------------------------------------
      9000    Ueber den Dächern Berlins Zentrum    POINT(13.334436985013 52.4979779501538)    10777             POLYGON((13.3318284987227 52.4956021172799,...
  • Query the popular accommodations in Berlin, group the accommodations by ZIP code, and then sort the groups by the order volume.

    • Sample code:

      SELECT  z.spatial_name AS zip
              ,COUNT(*) AS numAccommodations
      FROM    public.accommodations a
              ,public.zipcode z
      WHERE   ST_Within(a.shape, z.wkb_geometry)
      GROUP BY zip
      ORDER BY numAccommodations DESC
      ;
    • Returned result:

      zip      numaccommodations
      ----------------------------
      10245    872
      10247    832
      10437    733
      10115    664
      ...
      (187 rows returned)