Sql server 检查点是否在多边形内不工作
使用SQL Server 2012,以下查询不断告诉我,我要查找的点不在我使用的多边形内,而我知道它在。使用QGIS 2.2.0中的相同数据,我清楚地看到该点位于多边形内部(屏幕截图) 我需要做什么让查询告诉我点在多边形中?我看到一些搜索结果谈论“方向”,但我不知道如何指定它。坐标从Google Maps JavaScript API中捕获,并使用Entity Framework 6.1保存到数据库中 我想你有一个好主意。您需要反转多边形的顺序:Sql server 检查点是否在多边形内不工作,sql-server,sql-server-2012,sqlgeography,Sql Server,Sql Server 2012,Sqlgeography,使用SQL Server 2012,以下查询不断告诉我,我要查找的点不在我使用的多边形内,而我知道它在。使用QGIS 2.2.0中的相同数据,我清楚地看到该点位于多边形内部(屏幕截图) 我需要做什么让查询告诉我点在多边形中?我看到一些搜索结果谈论“方向”,但我不知道如何指定它。坐标从Google Maps JavaScript API中捕获,并使用Entity Framework 6.1保存到数据库中 我想你有一个好主意。您需要反转多边形的顺序: using Microsoft.SqlServe
using Microsoft.SqlServer.Types;
using System;
using System.Collections.Generic;
using System.Data.Spatial;
using System.Data.SqlTypes;
namespace SomeNamespace
{
public static class DbGeographyHelper
{
// 4326 is most common coordinate system used by GPS/Maps
// 4326 format puts LONGITUDE first then LATITUDE
private static int _coordinateSystem = 4326;
public static DbGeography CreatePolygon(string wktString)
{
// create polygon is same order as wktString
var sqlGeography = SqlGeography
.STGeomFromText(new SqlChars(wktString), _coordinateSystem)
.MakeValid();
// create the polygon in the reverse order
var invertedSqlGeography = sqlGeography.ReorientObject();
// which ever one is big is probably not the one you want
if (sqlGeography.STArea() > invertedSqlGeography.STArea())
{
sqlGeography = invertedSqlGeography;
}
return DbSpatialServices.Default.GeographyFromProviderValue(sqlGeography);
}
}
}
@Erik的回答是正确的,因为SQL Server排除了我在多边形中指定的区域。反转多边形修复了该问题。他的代码是正确的,但我必须进行修改以适应我的使用,因此,以下是我的代码,供关心我的人使用:
public static class DbGeographyExtensions {
public static DbGeography PolygonFromGoogleMapsText(
string wellKnownText,
int coordinateSystemId) {
SqlGeography geography = SqlGeography.STGeomFromText(new SqlChars(wellKnownText), coordinateSystemId).MakeValid();
SqlGeography invertedGeography = geography.ReorientObject();
if (geography.STArea() > invertedGeography.STArea()) {
geography = invertedGeography;
}
return DbGeography.FromText(geography.ToString(), coordinateSystemId);
}
}
我试图从@Erik的代码返回时遇到异常,因此我使用了下面答案中的代码:
public static class DbGeographyExtensions {
public static DbGeography PolygonFromGoogleMapsText(
string wellKnownText,
int coordinateSystemId) {
SqlGeography geography = SqlGeography.STGeomFromText(new SqlChars(wellKnownText), coordinateSystemId).MakeValid();
SqlGeography invertedGeography = geography.ReorientObject();
if (geography.STArea() > invertedGeography.STArea()) {
geography = invertedGeography;
}
return DbGeography.FromText(geography.ToString(), coordinateSystemId);
}
}