Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have a question about SQL Server and Geography functions. I'm looking to get the Centroid of a specific shape within a KML file.

Here's my little query

SQL
DECLARE @h VARCHAR(MAX);

    set @h = 'SELECT [Id]
          ,[Name]
          ,[placemark_sd_id]
          ,[ACC]
          ,[EXS]
          ,[NAM]
          ,[TXT]
          ,[UID_1]
          ,[placemark]
      FROM [dbKMLTemp].[dbo].[myTable]
      where [NAM] = "G";'

    DECLARE @g geometry;
    SET @g = geometry::STGeomFromText (@h,0)
    select @g.STCentroid().ToString();


I get this error.

Msg 6522, Level 16, State 1, Line 6
A .NET Framework error occurred during execution of user-defined routine or aggregate "geometry":
System.FormatException: 24114: The label SELECT [Id],[Name],[ in the input well-known text (WKT) is not valid. Valid labels are POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION, CIRCULARSTRING, COMPOUNDCURVE, CURVEPOLYGON and FULLGLOBE (geography Data Type only).
System.FormatException:
at Microsoft.SqlServer.Types.OpenGisTypes.ParseLabel(String input)
at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type)
at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeometry.GeometryFromText(OpenGisType type, SqlChars text, Int32 srid)

What I'm trying to do is take the following query but where it says POLYGON I want to get the polygon from a select statement:

SQL
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g.STCentroid().ToString();


I got this from https://docs.microsoft.com/en-us/sql/t-sql/spatial-geometry/stcentroid-geometry-data-type

How do I merge the two? I have a polygon from the KML file and now I need to find the center of it.

Thanks

Mike

What I have tried:

Look at my code in the question.
Posted
Updated 18-Jun-17 9:46am

It is impossible to help you.
Quote:
Msg 6522, Level 16, State 1, Line 6
A .NET Framework error occurred during execution of user-defined routine or aggregate "geometry":
System.FormatException: 24114: The label SELECT [Id],[Name],[ in the input well-known text (WKT) is not valid. Valid labels are POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION, CIRCULARSTRING, COMPOUNDCURVE, CURVEPOLYGON and FULLGLOBE (geography Data Type only).

With a careful reading of the error message, you should have seen that the error is not in SQL.
 
Share this answer
 
Comments
mzrax 18-Jun-17 14:39pm    
It's impossible to help me if you don't know what you're talking about.

Then how do you get the center point of a polygon from a query of selected points?
RedDk 18-Jun-17 14:47pm    
You're absolutely right about not knowing what they're talking about and I'd add that PM is a point grubber but that might be construed as starting a flame war here on CP. All that legalese aside, I think your query is wrong because it is SELECTg many things. Try SELECTg the thing to which the "geometry" (or "geography" as the case might be) is being applied. That ALONE.
Here's the solution.

A little messed up but now I'm starting to understand it. Thanks for everyone's help.
SQL
DECLARE @h geography;

set @h = (SELECT [placemark] FROM [dbKMLTemp].[dbo].[myTable]  where [NAM] = 'G')

DECLARE @g geometry;
SET @g = geometry::STGeomFromWKB(@h.STAsBinary(),4326)
select @g.STCentroid().ToString();


Initially I was selecting too many columns and I wasn't using STGeomFromWKB.

Mike
 
Share this answer
 
Comments
RedDk 20-Jun-17 18:32pm    
I was looking at this today and noticed that you've declared 'geography' but set the variable as if it's potentially 'geometry' which I guess is cool because when I try working this equality I use 'geometry' but because I have treated the binary as 'geography', translating text from longitude and latitude, I can't use STCentroid() ... there's no such thing. But I have to assume that @h is indeed geography and using the SRID 4326 worked! Which is mystifying because ... only you can see what that SELECT statement is refering to in myTable. What is the data type of [NAM]?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900