· 8 years ago · Aug 26, 2018, 06:06 PM
1make my Json string parsed openlayers.format.geojson
2json = "{ "type": "FeatureCollection", "features": ["
3
4 layer.get_feature_count.times do |i|
5 feature = layer.get_feature(i)
6 json += "{"type":"Feature", "id":" + feature.get_fid.to_s + ", "properties": {"
7
8 feature_defn = layer.get_layer_defn
9
10 feature_defn.get_field_count.times do |j|
11 field = feature_defn.get_field_defn(j)
12 json += """ + field.get_name_ref + "":" + """ + feature.get_field(j).to_s + """
13
14 if j+1 < feature_defn.get_field_count
15 json += ", "
16 end
17 end
18
19 json += "}, "
20 #end of properties
21
22 geom = feature.get_geometry_ref
23 json += ""geometry":" + geom.export_to_json
24 #end of geometry
25
26 json += "}"
27
28 if i+1 < layer.get_feature_count
29 json += ", "
30 end
31 end
32
33 json += "]}"
34
35CREATE TABLE IF NOT EXISTS `conditions` (
36 `conditionsID` int(10) unsigned NOT NULL auto_increment,
37 `ICAO` varchar(4) default NULL,
38 `LOCATION` varchar(50) default NULL,
39 `LATITUDE` float default NULL,
40 `LONGITUDE` float default NULL,
41 `TEMPERATURE` float default NULL,
42 `TEMPERATURE_F` float default NULL,
43 `TEMPERATURE_C` float default NULL,
44 `FEELS_LIKE` float default NULL,
45 PRIMARY KEY (`conditionsID`),
46 KEY `ICAO` (`ICAO`),
47) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=56274738 ;
48
49$feature_collection = array();
50$feature_collection['type'] = "FeatureCollection";
51$feature_collection['features'] = array();
52
53$con = mysql_connect($DB_URI, $DB_USER, $DB_PASS);
54if (!$con) {
55 $errorMessage = "Error: Could not connect to data database. Error: " . mysql_error();
56} else {
57 mysql_select_db("data", $con);
58 $result = mysql_query("SELECT * FROM `conditions` WHERE LATITUDE > $LAT AND LONGITUDE > $LON GROUP BY LOCATION;");
59
60 while ($row = mysql_fetch_assoc($result)) {
61 $feature_collection['features'][] = createFeature($row['conditionsID'],
62 $row['LATITUDE'],
63 $row['LONGITUDE'],
64 $row);
65 }
66}
67
68
69echo json_encode($feature_collection);
70
71function createFeature($ID, $lat, $lon, $data)
72{
73 unset($data["LATITUDE"]);
74 unset($data["LONGITUDE"]);
75 $feature = array();
76 $feature["type"] = "Feature";
77 $feature["id"] = $ID;
78
79 $feature["geometry"] = array();
80 $feature["geometry"]["type"] = "Point";
81 $feature["geometry"]["coordinates"] = array($lon+0, $lat+0);
82
83 $feature["properties"] = $data;
84 $feature["properties"]["visible"] = "true";
85
86 return $feature;
87}