Go to content Go to navigation and search

Home

Current SQL Server Blog Articles

    STGeometryTypes: Extracting all geometry type string values from complex geometry
    STDensify: Densify (m)LineString or (m)Polygon geometry objects
    STInsertN: Insert single vertex into a geometry
    STUpdateN: Update (replace) a single vertex within a geometry object.
    STUpdate: Replace all points equal to the supplied point with replacement point.
    STDeleteN: Delete single vertex from geometry
    STDelete: Deleting vertices in geometry objects
    STFlipVectors: Normalize direction of linestring vectors
    STConvertToLineString: Extract LineStrings in GeometryCollection to create LineString
    STLine2Cogo: Converting LineStrings to COGO XML
    STCogo2Line: Creating (Multi)LineStrings geometries from COGO XML instructions
    STVectorize: Break Linestring/Polygon elements into 2 point vectors (or 3 point circular curves)
    STScale: Function to Scale a geometry object
    TSQL String Tokenizer Function for SQL Server
    STGeometry2MBR/STGeography2MBR: Compute and return MBR ordinates
    generate_series for SQL Server 2008
    STExtractPolygon: Extract Polygons from result of STIntersection in SQL Server Spatial
    STRound: Function to round ordinates of a SQL Server Spatial geometry object
    STExtract: Extract elements of a geometry object
    STNumRings: Counting number of polygon rings
    STFilterRings: Removing rings from Polygon based on area.
    STMove: Function to Move a geometry object in SQL Server Spatial
    STCentroid*: Alternate Functions for Compute a Centroid
    STRotate: Function to rotate a geometry object in SQL Server Spatial
    STVertices: Wrapper over STDumpPoints
    STMorton: Creating a Morton number Space Key value for grid square
    Gridding a geometry or geography object (SQL Server Denali)
    On hinting spatial indexes
    RandomSearchByExtent: Random Search Procedure (2008 Spatial)
    COGO: Convert DMS String to decimal degrees floating point number.
    COGO: Converting (Google Earth) Formatted Longitude/Latitude points to decimal degrees (SQL Server)
    COGO: Convert Degrees, Minutes and Seconds values to Decimal Degrees
    COGO: DD2DMS Formatting a latitude/longitude decimal degree value
    COGO: Create point from bearing and distance
    COGO: Compute number of vertices required to stroke circle or circular arc
    COGO: Calculating the bearing between two points (SQL Server 2008 Spatial)
    COGO: Compute arc length subtended by angle centre of circle
    COGO: Compute chord length of segment of a circle (arc)
    COGO: Computing Arc To Chord (Arc2Chord) Separation
    COGO: Creating a stroked polygon from a circle's centre x,y and radius
    COGO: Finding centre and radius of a curve defined by three points: FindCircle function
    CheckRadii: Identifying Tight Radius Curves sections within LineString geometry data
    Generating random point data for SQL Server 2008 Spatial
    New Presentation on Active (Searchable) Spatial Metadata for SQL Server 2008 Spatial and FreeText
    STisGeo: Checking if a column in a table or a view is of type geometry or geography
    Vectorising geometry objects in SQL Server 2008
    STMBR2Geometry/STMBR2Geography functions
    Extracting geometry types from geometry/geography objects in SQL Server 2008
    Getting the number of coordinate dimensions of a geometry/geography object in SQL Server 2008
    STDumpPoints: A Function that Dumps the Vertices/Points in a geometry
    toGeography and toGeography Conversion Functions for SQL Server 2008 Spatial
    Write text file with spatial data from SQL Server 2008
    Loading Shapefiles into Geography type column in SQL Server 2008
    Tip #6: Correcting invalid geometries
    Tip #5: Where or where has my little column gone
    Tip #4: What Coordinate System is that?
    Tip #3: What object is that?
    Tip #2: Spatial Indexing and Primary Keys
    Tip #1: SQL Server 2008 "Katmai" - Setting SRIDs

Search

    Browse

RSS / Atom

Email me

textpattern

Creative Commons License
All Blog Articles, Data Models and Free Source Code by Simon Greener, The SpatialDB Advisor is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

STMBR2Geometry/STMBR2Geography functions

Monday September 28 2009 at 10:45

KeywordsMBR 5 Vertex Polygon.
Summary

Convert Lower Left / Upper Right coordinates into a 5 vertex polygon

Oracle has a polygon geometry type (non-standard) called an optimized rectangle. To define one all one has to do is supply it with the lower-left and upper-right coordinates. This is a much easier thing to do than having to supply 5 coordinates in anti-clockwise order.

PostGIS also has a bunch of bounding box functions that also are useful for when working with rectangular polygons or minimum bounding rectangles (MBRs).

I have been playing around a lot, recently, with grid generation in SQL Server 2008 (more on this in another post). And, in doing so, I decided to code some functions that would return SQL Server 2008 geometry and geography polygon objects given the lower-left and upper-right coordinates of a minimum bounding rectangle.

Here they are.

  1. USE [GISDB]  -- Change this to your database
  2. GO
  3. -- Function that operates on Geography data type.
  4. CREATE FUNCTION [dbo].[STMBR2Geography] (
  5.   @p_minx  FLOAT,
  6.   @p_miny  FLOAT,
  7.   @p_maxx  FLOAT,
  8.   @p_maxy  FLOAT,
  9.   @p_srid  INT )
  10.   RETURNS geography
  11. AS
  12. BEGIN
  13.   RETURN geography::STGeomFromText('POLYGON((' +
  14.                  CONVERT(VARCHAR(100), CAST(@p_minx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_miny AS DECIMAL(24,12))) + ',' +
  15.                  CONVERT(VARCHAR(100), CAST(@p_maxx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_miny AS DECIMAL(24,12))) + ',' +
  16.                  CONVERT(VARCHAR(100), CAST(@p_maxx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_maxy AS DECIMAL(24,12))) + ',' +
  17.                  CONVERT(VARCHAR(100), CAST(@p_minx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_maxy AS DECIMAL(24,12))) + ',' +
  18.                  CONVERT(VARCHAR(100), CAST(@p_minx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_miny AS DECIMAL(24,12))) + '))',
  19.                  @p_srid);
  20. END;
  21. GO
  22. -- Same function but for Geometry type
  23. CREATE FUNCTION [dbo].[STMBR2Geometry](
  24.   @p_minx  FLOAT,
  25.   @p_miny  FLOAT,
  26.   @p_maxx  FLOAT,
  27.   @p_maxy  FLOAT,
  28.   @p_srid  INT )
  29.   RETURNS geometry
  30. AS
  31. BEGIN
  32.   RETURN geometry::STGeomFromText('POLYGON((' +
  33.                  CONVERT(VARCHAR(100), CAST(@p_minx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_miny AS DECIMAL(24,12))) + ',' +
  34.                  CONVERT(VARCHAR(100), CAST(@p_maxx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_miny AS DECIMAL(24,12))) + ',' +
  35.                  CONVERT(VARCHAR(100), CAST(@p_maxx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_maxy AS DECIMAL(24,12))) + ',' +
  36.                  CONVERT(VARCHAR(100), CAST(@p_minx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_maxy AS DECIMAL(24,12))) + ',' +
  37.                  CONVERT(VARCHAR(100), CAST(@p_minx AS DECIMAL(24,12))) + ' ' + CONVERT(VARCHAR(100), CAST(@p_miny AS DECIMAL(24,12))) + '))',
  38.                  @p_srid);
  39. END;
  40. GO

Testing

Testing is pretty easy.

  1. -- First, let's create a simple polygon geometry
  2. --
  3. SELECT dbo.STMBR2Geometry(0,0,100,100,0).STAsText() AS geomWKT;

geomWKT
POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0))

  1. -- Now, let's create a polygon with a hole
  2. --
  3. SELECT dbo.STMBR2Geometry(0,0,100,100,0).STDifference(dbo.STMBR2Geometry(40,40,60,60,0)).STAsText() AS geomWKT;

geomWKT
POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0), (40 40, 40 60, 60 60, 60 40, 40 40))

  1. -- Now let's create a multipolygon with a hole
  2. --
  3. SELECT dbo.STMBR2Geometry(0,0,100,100,0).STDifference(dbo.STMBR2Geometry(40,40,60,60,0)).STUnion(dbo.STMBR2Geometry(200,200,400,400,0)).STAsText() AS geomWKT;

geomWKT
MULTIPOLYGON (((200 200, 400 200, 400 400, 200 400, 200 200)), ((0 0, 100 0, 100 100, 0 100, 0 0), (40 40, 40 60, 60 60, 60 40, 40 40)))

  1. -- Finally, let's create a polygon with a hole using the STMBR2Geography function
  2. --
  3. SELECT dbo.STMBR2Geography (147,-44,148,-43,4326)
  4.           .STDifference(dbo.STMBR2Geography (147.4,-43.6,147.6,-43.2,4326)).STAsText() AS geogWKT;

geogWKT
POLYGON ((147 -44, 148 -44, 148 -43, 147 -43, 147 -44), (147.40000000596046 -43.599999994039536, 147.40000000596046 -43.200000002980232, 147.59999999403954 -43.200000002980232, 147.59999999403954 -43.599999994039536, 147.40000000596046 -43.599999994039536))

I hope someone finds these functions useful.

Creative Commons License

post this at del.icio.uspost this at Diggpost this at Technoratipost this at Redditpost this at Farkpost this at Yahoo! my webpost this at Windows Livepost this at Google Bookmarkspost this to Twitter

Comment