使用准备好的语句插入GIS几何体

时间:2011-12-07 10:34:36

标签: php mysql pdo gis

有没有办法使用预准备语句将GIS数据插入mysql数据库(使用PHP / PDO)?

这是失败的:

$stmt = $sql->prepare("INSERT INTO geo SET location= :loc");
$sql->execute($stmt,array('loc'=>"PointFromText('POINT(43.5499152 7.0232372)')"));

列geo.location的类型为POINT。

2 个答案:

答案 0 :(得分:3)

纬度是43.5499152而经度是7.0232372吗?如果是这样,考虑到POINT的WKT格式是POINT(X Y),其中X是经度,Y是纬度,有效的表示将是:

POINT(7.0232372 43.5499152)

考虑到这一点,您可以尝试以下方式:

$stmt = $sql->prepare("INSERT INTO geo SET location = PointFromText('POINT(:lng :lat)')");
$stmt->execute(array('lat' => 43.5499152, 'lng' => 7.0232372));

如果您没有纬度和经度,只需将location设置为null:

$stmt = $sql->prepare("INSERT INTO geo SET location = null");
$stmt->execute();

另外,您可能想要设置SRID,在您的情况下为4326:

$stmt = $sql->prepare("INSERT INTO geo SET location = PointFromText('POINT(:lng :lat)', 4326)");
$stmt->execute(array('lat' => 43.5499152, 'lng' => 7.0232372));

答案 1 :(得分:0)

@Glyn:看到以下为我工作

insertquery = <do as required>
Your insert query should have the following:
"geometry::STGeomFromText(?,4326))" 


PreparedStatement insert = conn.prepareStatemen(insertquery);

#Point has two points, X and Y. You need to pass those values.
String shape = "POINT ("+x+" "+y+")";
insert.setString(25, shape);

您使用的数据库,例如MS sql server,然后您的point列应该具有正确的数据类型“Geometry”。

请问我任何进一步的问题。

很乐意提供帮助。

相关问题