Go to content Go to navigation Go to search

Featured Articles:

    What sorts of Services do I offer?
    What people say about me or my work...
    Book: "Applying And Extending Oracle Spatial"
    Analyzing Spatial Query Performance Improvements in Oracle Spatial and Graph 12c Through Cross-Vendor Comparison

Newest Article:

STGeometryTypes: Extracting all geometry type string values from complex geometry

Wednesday May 01 2019 at 16:56

Ever needed to extract a textual description of all geometry types in a single geometry/geography in SQL Server 2008 inluding all sub-elements?

Recently I had cause to do so and created the following function that implements this need.

  1. USE [GISDB]  -- Change this to your database
  2. GO
  3. --
  4. -- Extracts all geometry type keywords from a geometry (and its sub-elements)
  5. CREATE FUNCTION [$(owner)].[STGeometryTypes](
  6.   @p_geometry geometry
  7. )
  8.  RETURNS VARCHAR(MAX)
  9. AS
  10. BEGIN
  11.   DECLARE
  12.     @v_return VARCHAR(MAX);
  13.   BEGIN
  14.     IF ( @p_geometry IS NULL )
  15.       RETURN NULL;
  16.     SELECT @v_return = SUBSTRING(a.token,2,LEN(a.token))
  17.       FROM (SELECT (STUFF((SELECT ',' + a.token
  18.                              FROM (SELECT t.token
  19.                                      FROM [$(owner)].[Tokenizer](@p_geometry.AsTextZM(),' ,)(') AS t
  20.                      ) AS a
  21.                             WHERE a.token IS NOT NULL
  22.                               AND a.token NOT LIKE '%[0-9]%'
  23.                            FOR XML PATH(''), TYPE, ROOT
  24.               ).VALUE('root[1]','nvarchar(max)'),1,1,'''')
  25.                     ) AS token
  26.             ) AS a;
  27.     RETURN @v_return;
  28.    END;
  29. END
  30. GO

Now let’s test it against a selection of all possible geometry types

  1. -- Simple geometry
  2. SELECT dbo.[STGeometryTypes](geometry::STGeomFromText('POINT(0 1 2)',0)) AS gtypes;
  3. GO
  4. gtypes
  5. POINT
  6. -- Single CurvePolygon with one interior ring
  7. SELECT [$(owner)].[STGeometryTypes](geometry::STGeomFromText('CURVEPOLYGON(CIRCULARSTRING(0 5, 5 0, 0 -5, -5 0, 0 5), (-2 2, 2 2, 2 -2, -2 -2, -2 2))',0)) AS gtypes;
  8. GO
  9. gtypes
  10. CURVEPOLYGON,CIRCULARSTRING
  11. -- GeometryCollection
  12. SELECT  [$(owner)].[STGeometryTypes](
  13. geometry::STGeomFromText(
  14.          'GEOMETRYCOLLECTION(
  15.                  LINESTRING(0 0,20 0,20 20,0 20,0 0),
  16.                  CURVEPOLYGON(
  17.                       COMPOUNDCURVE(
  18.                               (0 -23.43778, 0 23.43778),
  19.                               CIRCULARSTRING(0 23.43778, -45 23.43778, -90 23.43778),
  20.                               (-90 23.43778, -90 -23.43778),
  21.                               CIRCULARSTRING(-90 -23.43778, -45 -23.43778, 0 -23.43778))),
  22.                  COMPOUNDCURVE(
  23.                          CIRCULARSTRING(-90 -23.43778, -45 -23.43778, 0 -23.43778),
  24.                          (0 -23.43778, 0 23.43778)))',0)) AS gTypes;
  25. GO
  26. gTypes
  27. GEOMETRYCOLLECTION,LINESTRING,CURVEPOLYGON,COMPOUNDCURVE,CIRCULARSTRING,CIRCULARSTRING,COMPOUNDCURVE,CIRCULARSTRING

I hope this function is useful to someone.

My Services

I offer the following services to customers:

  1. Integration of GeoSpatial within IT
    Advice and guidance on how to fully integrate geospatial data and processing needs within existing, pure, IT environments (using existing GIS software or without purchasing proprietary GIS software);
  2. Independent Solutions Advice and Sanity Checking
    Solutions not Software ;
    How to efficiently and effectively solve a business requirement via existing technology;
    Does what you have been offered in a tender really stack up? Or do you want independent quality assurance of a tender document before submitting it to a customer?
    When it is best to use Open Source as against Commercial off the shelf (COTS) and vice versa?
    Is buzzword-compliant (SOA, BPEL, J2EE, .NET, WebServices…) technology really needed to solve a business problem? If so, what is the best way to achieve this?
    ;
  3. Analyzing ROI of your current GIS software technology
    Matching business problems to existing software investment. Are you getting value from your software investment? How to improve that ROI;
  4. Systems Review
    Does the solution you have been provided actually match your requirements?;
  5. Database Benchmarking:
    How well are you using your Oracle Spatial/SQL Server Spatial/PostGIS database?
    How good is your databases documentation/metadata?
    Is the integrity and quality of the data stored in your databases as good as you think?
    Is your use of Oracle (Spatial), SQL Server Spatial consistent with industry best-practice?
    Get advice on some simple “rules” to follow to improve your database!
  6. Database Consulting through design, test, build and configuration services
    Ensuring your databases reflect your needs via:
    1. Data Quality Rules Documentation, Conformance Testing, Correction and Certification;
    2. Data Conversion, Migration, Loading;
    3. Database Design;
    4. Coding of appropriate in-Database processing:
      Via design and coding services in:
      1. PL/SQL – Oracle,
      2. PL/pgSQL – PostgreSQL,
      3. T-SQLSQL Server,
      4. Java,
      5. C#.
    5. Database Performance Tuning;
    6. Spatially enabling Data Warehouses and Processes;
    7. ArcSDE Configuration and Tuning.
  7. SpatialDB Training Courses and Staff Mentoring
    Spatial SQL Training on your datasets;
    After Training support
    ;
  8. Free Software
  9. Commercial Support
    GeoRaptor
    Free PL/SQL packages
    (bug fixes, extensions and new functionality).

I have over 25 years experience with database technologies from mainframes through to personal computers. I have 25 years Oracle use and am recognised by the Oracle Spatial development team as one of the early adopters of Oracle’s Sdo_Geometry data type. In 2011 that experience was recognised when Oracle awarded me the “2011 Oracle Spatial Excellence Award for Education and Research”.

I was on the original beta program for SQL Server 2008 Spatial (aka “Katmai”). While most of my solutions work centres on Oracle, I now have quite a few years experience designing, programming, tuning and working with SQL Server. I make available over 200 TSQL functions to SQL Server Spatial users across multiple versions.

I also have the necessary PostgreSQL/PostGIS and MySQL experience to help you implement the best spatial database solution possible.

I have (next to the original designer and programmer) the longest experience of using ArcSDE in the industry and can help you get the best out of this piece of ESRI software and show you how to make it work with other spatial software (esp open source). Also, with the new ESRI Spatial Type for Oracle, PostgreSQL and SQL Server, I am in a unique position to help you plan your migrations from SDEBINARY to either the ESRI Spatial Type, SQL Server spatial or Oracle’s Sdo_Geometry.

My experience is not limited just to databases. I have managed “GIS” infrastructure management teams, designed and built systems integrating spatial processing within business systems in the areas of mapping, editing, analysis, GPS field data capture etc.

Finally, as a geospatial professional with IT and Oracle (Certified Professional SQL 10gR2; Certified Associate DBA 10gR2) qualifications and experience, I can help guide the uptake of geospatial technologies into business’s IT infrastructures.

Let me help you cost-effectively spatially-enable your enterprise.