使我的Json字符串解析为openlayers.format.geojson



我需要将Json字符串更改为Geojson格式。现在我正在使用Json_encode((函数将sql查询结果转换为Json字符串(如此链接中所示,Json编码MySQL结果(
我想让这个geojson字符串能够被openlayers读取
如何使用jsonencode实现此目的?或者我可以用一个不同的函数吗?提前谢谢。

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 += "]}"

(我从您的帖子中假设您使用的是PHP(。

如果将空间数据作为WKT(1(获取,则可以使用PHP版本的mapfish中的GeoJSON类(2((该类可以与mapfish一起使用(。

您只需要为此编写自己的适配器。

HTH,

  • 1http://dev.mysql.com/doc/refman/5.0/en/functions-to-convert-geometries-between-formats.html#function_astext
  • 2https://github.com/tonio/sfMapFishPlugin/tree/master/lib/GeoJSON

在过去的几周里,我实际上一直在处理同样的问题。需要注意的是:我没有使用地理空间数据库,对我来说只是普通的存储(准确地说是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"函数可以让我快速构建一个功能。另外请注意,我将几乎整行添加到properties对象中。过杀/冗余,但使代码更简单。

希望这个片段能有所帮助。这是我的工作(我很乐意听取别人的建议(。

最新更新