Using GDAL/OGR Guide space data to SQLServer2008

Source: Internet
Author: User

There is a tool on the Internet can do, but the import of Chinese is garbled, search a circle seems to have no solution, so want to do one, hey, a mess to know the trouble is heavy AH ~

First look at the data, understand that the spatial data in SQL Server is divided into two kinds, geometry and geography, a plane is a spherical surface, for example, in doing distance calculation results are not the same AH

So I have to geography the spherical coordinate system, but this thing has limitations, such as the polygon outer ring direction must be counterclockwise, the inner ring must be clockwise, not more than a hemisphere and so on

Regardless of the first to do, load gdal and org, open the MapInfo layer, traversal, take the field normal, take the correct number of geometry, the next step will be the geometry of data export, a lot of ways to support the OpenGIS system

Support WKT and WKB, to facilitate the selection of exporttowkt, and then generate INSERT statements, almost, execute, the problem is: SQL Server error, say what

Message 6522, Level 16, State 1, line 1th

A. NET Framework error occurred during the execution of a user-defined routine or an aggregation of "geography":

MICROSOFT.SQLSERVER.TYPES.GLARGUMENTEXCEPTION:24205: Because the specified input exceeds a hemisphere, it does not represent a valid geographic instance. Each geographical instance must be able to be accommodated within a hemisphere. A common cause of this error is a circular direction error for a polygon.

Microsoft.SqlServer.Types.GLArgumentException:

In Microsoft.SqlServer.Types.GLNativeMethods.ThrowExceptionForHr (Gl_hresult errorcode)

In Microsoft.SqlServer.Types.GLNativeMethods.GeodeticIsValid (Geodata g)

In Microsoft.SqlServer.Types.SqlGeography.IsValidExpensive ()

In Microsoft.SqlServer.Types.SqlGeography.ConstructGeographyFromUserInput (geodata G, Int32 Srid)

In Microsoft.SqlServer.Types.SqlGeography.GeographyFromText (opengistype type, SqlChars taggedtext, Int32 Srid)

In Microsoft.SqlServer.Types.SqlGeography.STGeomFromText (SqlChars geometrytaggedtext, Int32 Srid)

Will geography::stgeomfromtext replaced by Geometry::stgeomfromtext, no problem can be normal generation of geometry, hey, show the true ring direction of the problem, change it, so with the next code

Using System;

Using System.Collections.Generic;

Using System.Text.RegularExpressions;

Namespace Sqlspatialtools

{

public class Point

{

Public double X {get; set;}

Public double Y {get; set;}

Public point (Double x, double y)

{

x = x;

y = y;

}

public static point Parse (String pt)

{

var p = Pt. Split (new[] {""}, Stringsplitoptions.removeemptyentries);

return p.length = = 2? New Point (Convert.todouble (p[0]), convert.todouble (p[1)): null;

}

public override string ToString ()

{

return string. Format ("{0:f8} {1:f8}", X, Y);

}

}

public static Class Wkt

{

///

To determine if the ring is in a clockwise direction

///

///

True clockwise, false counterclockwise

private static bool Isringclockwise (IList PP)

{

Double T = 0;

for (var i = 1; i < pp.) Count; i++)

T + + pp[i-1]. X * Pp[i]. Y-pp[i]. X * Pp[i-1]. Y

T + + pp[pp. COUNT-1]. X * Pp[0]. Y-pp[0]. X * pp[pp. COUNT-1]. Y

return T < 0;

}

Judge whether the ring

public static bool Isring (IList PP)

{

var t = pp. Count-1;

Return (T > 2 && pp[0]. X = = Pp[t]. X && pp[0]. Y = = Pp[t]. Y);

}

public static string[] Resverse (List pts, bool resverse)

{

var sl = new List ();

foreach (Var pt in pts)

{

Sl. Add (Pt. ToString ());

}

if (resverse)

Sl. Reverse ();

Return SL. ToArray ();

}

public static string Reversewkt (String wkt)

{

if (! Regex.IsMatch (wkt, "POLYGON"))

return wkt;

var retwkt = wkt;

var ms = Regex.Matches (retwkt, @ "(d+.*?d+)");

According to the geography rule, the outer ring of the polygon should be counterclockwise and the inner loop clockwise

POLYGON (()) in the first () is the shape, the other is the inner ring

LINESTRING () directly to determine whether it is counterclockwise (to determine the need to counter-clockwise)

if (Ms. Count > 0)

{

for (var i = Ms.) Count-1; I >= 0; i--)

{

var m = ms[i];

var s = m.tostring (). TrimStart (new[] {'} '). TrimEnd (new[] {') '});

var pa = s.split (new[] {","}, Stringsplitoptions.removeemptyentries);

var ls = new List ();

foreach (Var p in PA)

{

var pt = Point.parse (p);

if (pt!= null)

Ls. Add (PT);

}

The TODO polygon has an inner loop that cannot be reversed, clockwise

if (isring (LS))

{

var rs = string. Join (",", Resverse (LS, isringclockwise (LS));//Reverse if clockwise

Retwkt = retwkt. Remove (M.index, m.length). Insert (M.index, String. Format ("({0})", RS));

}

}

return retwkt;

}

return wkt;

}

}

}

The above. NET 2.0 is string[]. ToArray This is not supported, although Mutipolygon and geomertycollection are still uncertain, but the general polygon is no problem, again tested a MapInfo Table

(Chinese provincial) basic OK, and then guide surface boundaries, some of the results are uncertain, export wkt with select in Sqler to try, really not ah, check the online tool guide records, WKT is geometrycollection, and I use gdal/ Ogr found out it's polygon type.

This is clearly a line and a polygon composed of Ah, Gdal/ogr incredibly said is polygon, tried GDAL1.6, GDAL1.7, should be the problem of ogr, do not make, tired feel not love AH

In short, the geometry mode for ESRI shape is still OK, the Chinese problem is solved, and SQL Server's UPDATE statement can basically solve the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.