我需要将 Json 字符串更改为 Geojson 格式。现在我正在使用 Json_encode() 函数将 sql 查询结果转换为 json 字符串(如此链接中给出的JSON 编码 MySQL 结果)。
我想让这个geojson字符串被openlayers读取。
我如何为此目的使用 json _encode ?或者我可以使用其他功能吗?提前致谢。
3 回答
(我从您的帖子中假设您正在使用 PHP)。
如果您以 WKT (1) 的形式获取空间数据,则可以使用 PHP 版本的 mapfish 中的 GeoJSON 类 (2)(该类可以在没有 mapfish 的情况下使用)。
您只需要为此编写自己的适配器。
高温下,
GeoJSON 格式相当简单,因此我将继续遍历 SQL 查询返回的所有原始数据,并手动构建 GeoJSON 格式的字符串。然后将此字符串从服务器端代码传递到客户端,在客户端将其解析为 javascript 对象,以便 OpenLayers 可以使用它。
这是我不久前用 Ruby 编写的示例。希望你明白这个想法并且可以在 PHP 中做同样的事情:
json = "{ \"type\": \"FeatureCollection\", \"features\": ["
layer.get_feature_count.times do |i|
feature = layer.get_feature(i)
json += "{\"type\":\"Feature\", \"id\":" + feature.get_fid.to_s + ", \"properties\": {"
feature_defn = layer.get_layer_defn
feature_defn.get_field_count.times do |j|
field = feature_defn.get_field_defn(j)
json += "\"" + field.get_name_ref + "\":" + "\"" + feature.get_field(j).to_s + "\""
if j+1 < feature_defn.get_field_count
json += ", "
end
end
json += "}, "
#end of properties
geom = feature.get_geometry_ref
json += "\"geometry\":" + geom.export_to_json
#end of geometry
json += "}"
if i+1 < layer.get_feature_count
json += ", "
end
end
json += "]}"
在过去的几周里,我实际上一直在研究同样的问题。需要注意的重要一点:我没有使用地理空间数据库,只是对我来说是正常的 ol' 存储(确切地说是 MySQL)。
这是我完成它的方法(没有凌乱的 concat 语句):
SQL 表说明:
CREATE TABLE IF NOT EXISTS `conditions` (
`conditionsID` int(10) unsigned NOT NULL auto_increment,
`ICAO` varchar(4) default NULL,
`LOCATION` varchar(50) default NULL,
`LATITUDE` float default NULL,
`LONGITUDE` float default NULL,
`TEMPERATURE` float default NULL,
`TEMPERATURE_F` float default NULL,
`TEMPERATURE_C` float default NULL,
`FEELS_LIKE` float default NULL,
PRIMARY KEY (`conditionsID`),
KEY `ICAO` (`ICAO`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=56274738 ;
然后我可以使用 json_encode PHP 库(在 5.2 中首次提供,5.3 的更好选项)从 PHP 对象编码 json 对象。不再使用字符串,而是可以使用伪对象(我更喜欢数组,易于动态管理)。5.3 也带来了漂亮打印的选项。
我的 PHP 服务器代码:
$feature_collection = array();
$feature_collection['type'] = "FeatureCollection";
$feature_collection['features'] = array();
$con = mysql_connect($DB_URI, $DB_USER, $DB_PASS);
if (!$con) {
$errorMessage = "Error: Could not connect to data database. Error: " . mysql_error();
} else {
mysql_select_db("data", $con);
$result = mysql_query("SELECT * FROM `conditions` WHERE LATITUDE > $LAT AND LONGITUDE > $LON GROUP BY LOCATION;");
while ($row = mysql_fetch_assoc($result)) {
$feature_collection['features'][] = createFeature($row['conditionsID'],
$row['LATITUDE'],
$row['LONGITUDE'],
$row);
}
}
echo json_encode($feature_collection);
function createFeature($ID, $lat, $lon, $data)
{
unset($data["LATITUDE"]);
unset($data["LONGITUDE"]);
$feature = array();
$feature["type"] = "Feature";
$feature["id"] = $ID;
$feature["geometry"] = array();
$feature["geometry"]["type"] = "Point";
$feature["geometry"]["coordinates"] = array($lon+0, $lat+0);
$feature["properties"] = $data;
$feature["properties"]["visible"] = "true";
return $feature;
}
请注意“FeatureCreate”功能,让我可以快速构建功能。另请注意,我几乎将整行添加到属性对象中。过度杀伤/冗余,但使代码更简单。
希望这个片段会有所帮助。它完成了我的工作(我很乐意听取其他人的建议)。