0

I have a WMS layer I serve from SQL View. When I GetFeatureInfo I receive JSON presentation of the feature.

{"type":"FeatureCollection","features":[{"type":"Feature","id":"Trips.fid--3a106d6c_13f7fcd4d00_-7dab","geometry":{"type":"MultiLineString","coordinates":[[[34.78731593803529,31.31823531417712],[34.78843141500525,31.336622682320776],[34.79414493381831,31.355201806839506],[34.794207563998924,31.371163684259038],[34.79296734448457,31.375581921229493],[34.78922614608776,31.390623411898762],[34.78827495215694,31.408575008023988],[34.78636890354401,31.42299930464733],[34.78632872060815,31.42193697540446],[34.78629782738711,31.422105913663092],[34.78628031267043,31.421992838029546],[34.786190597348,31.42158272671166],[34.78616186179376,31.421905927160644],[34.784898002487694,31.424062281290652],[34.773401914392345,31.434990008872095],[34.76559077292494,31.446642271859293],[34.77342394487551,31.466799593779264],[34.77975547041099,31.490976936335215],[34.78263023215561,31.506823501958817],[34.792679583505226,31.531054387260554],[34.80048435469499,31.55136414506193],[34.80654617304027,31.574946250797954],[34.813199698392914,31.59725795152748],[34.81470088275501,31.619577056013927],[34.809541545607935,31.657018778594132],[34.80638283046715,31.674769044083895],[34.81569739824674,31.69878758883678],[34.82117079659263,31.721351512377144],[34.822676820645704,31.74065914102294],[34.82640625397537,31.760366608188313],[34.83277797643619,31.795874550345438],[34.84013377052116,31.80349376296454],[34.85764369385628,31.809682398342275],[34.87655834935133,31.809001817509152],[34.89977578898199,31.81087415167135],[34.91555026001387,31.814641705371283],[34.92078885084598,31.819327945165384],[34.94872743329605,31.826006370096646],[34.96865364149424,31.831077555261356],[34.977436258261335,31.8348796713872],[34.988078880595516,31.834460104995458],[34.99298330536876,31.826840136006965],[35.00026754690508,31.826135241324245],[35.005568854286416,31.82592323790467],[35.01774054730234,31.8215162498958],[35.02155196628523,31.818288829834177],[35.02691352351924,31.813341112120234],[35.03143746024918,31.80979583988096],[35.03815953441021,31.80619724694477],[35.045420139243944,31.80474599411263],[35.05317138181679,31.804966181835955],[35.06486714082564,31.804141661709476],[35.07584180677903,31.800281337346696],[35.08838316507283,31.800341312456926],[35.10640165800501,31.801455597207628],[35.12610124373968,31.800681742893538],[35.14697634526269,31.79722164231699],[35.16219627795028,31.79329681701213],[35.171594439546396,31.797658570113885],[35.187671634314036,31.797928602082614],[35.188362774999014,31.79653341639412],[35.1887256549307,31.7961346714363],[35.19047234637513,31.794962708212374],[35.19256271051755,31.79461237466848],[35.19270785200568,31.79363734891888],[35.193583458825614,31.79312975679172],[35.194747974527196,31.793406487587923],[35.196095096006964,31.79164621288934],[35.196955729849186,31.790773244292048],[35.1978874456438,31.790160915552967],[35.198711620213004,31.78987941300083],[35.19929508848269,31.787229378909604],[35.19850752277577,31.785375424789216],[35.19941639636708,31.784687679081976],[35.199241688426945,31.78231397727036],[35.198391385845284,31.781402127099458],[35.201245278823734,31.77605635342819]]]},"geometry_name":"geom","properties":{"Id":1,"StartDateTime":"2013-06-23T01:49:57Z","EndDateTime":"2013-06-23T03:23:32Z","Length":94.57}},{"type":"Feature","id":"Trips.fid--3a106d6c_13f7fcd4d00_-7daa","geometry":{"type":"MultiLineString","coordinates":[[[35.20076873304309,31.77473722793188],[35.204546738625524,31.774719539653546],[35.208965075961345,31.77618510615382],[35.208640148891355,31.78202931583896],[35.20975512430616,31.78246578222451],[35.21137792550667,31.781691930524566],[35.213549710915274,31.780813498217512],[35.21476900302076,31.78168291327713],[35.21469230369844,31.780946053301573],[35.21257227234834,31.781205360240914],[35.20864390470302,31.783054725633296],[35.20829895121278,31.78302020473956],[35.203248270523765,31.78222448311647],[35.19854766107952,31.786334626536405],[35.19984531073088,31.78751601702663],[35.20014413481394,31.788041155898213],[35.19725069211512,31.790485693913038],[35.19110776321938,31.795067815242238],[35.18161040176461,31.801656830805612],[35.1687011405429,31.79567693197168],[35.159244819963,31.795794581845737],[35.11942475024883,31.798775201079778],[35.08039358590231,31.800856179659302],[35.063209196166355,31.805088407881986],[35.04805678839844,31.804031639631894],[35.032927570632985,31.80847877245744],[35.02092427914747,31.81877639146],[34.99012978758847,31.829259174717787],[34.98804795187599,31.839040689444193],[34.977978798901404,31.835101996624207],[34.9498888706375,31.82596783070529],[34.92736197558343,31.823111392882268],[34.91695636557349,31.815625240675068],[34.903688415937665,31.811906632219383],[34.88296941318597,31.810547012219338],[34.86002652341891,31.81005225691859],[34.8400240232646,31.803567891209664],[34.83015973660191,31.79428193887949],[34.832461959577664,31.781116935605244],[34.82711462326763,31.762582638502867],[34.82278272110514,31.743736272787874],[34.82054030348634,31.720919484741522],[34.81420911419586,31.695453086060326],[34.80627534157327,31.675224452093136],[34.81001464412418,31.653220249705704],[34.814807584458386,31.623882775804965],[34.81329122844819,31.60270186644005],[34.8080051217612,31.582810947810554],[34.80303812730152,31.560726326313713],[34.797198877391246,31.54151166321618],[34.786754234512,31.52322945275979],[34.78295267962934,31.504485052320216],[34.77614342612191,31.48399237576942],[34.77342018413583,31.467251749582854],[34.76645583238203,31.450437751509558],[34.76590528960303,31.442231443933725],[34.78525964273001,31.422175881020067],[34.78907352850801,31.401593669076053],[34.79129725926219,31.38162192285955],[34.795717357038875,31.365271563573277],[34.792079987685526,31.350441990935515],[34.786453724639145,31.329829087974357],[34.78839683025119,31.309436171497104],[34.79081076415792,31.289914211434404],[34.7976451665849,31.28794303366183],[34.80366544178834,31.286754464613146]]]},"geometry_name":"geom","properties":{"Id":2,"StartDateTime":"2013-06-23T04:46:14Z","EndDateTime":"2013-06-23T06:03:47Z","Length":102.14}}],"crs":{"type":"EPSG","properties":{"code":"4326"}}}

How can I add this item in OpenLayers to new vector layer (to simulate SELECTED style)?

1

You should parse server answer and add features to vector layer. Code example:

var epsg4326 = new OpenLayers.Projection("EPSG:4326");
var epsg900913 = new OpenLayers.Projection("EPSG:900913");
var jsonFormat = new OpenLayers.Format.GeoJSON({
    externalProjection: epsg4326,
    internalProjection: epsg900913
});

var options = {
    div: "map",
    zoom: 12,
    center: [3872506.295747729, 3674147.387632317],
    layers: [
        new OpenLayers.Layer.OSM()
    ]
};
var map = new OpenLayers.Map(options);
var vector = new OpenLayers.Layer.Vector('', {style: OpenLayers.Feature.Vector.style.select});
map.addLayer(vector);

var json = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "id": "Trips.fid--3a106d6c_13f7fcd4d00_-7dab",
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [
                    [
                        [
                            34.78731593803529,
                            31.31823531417712
                        ],
                        [
                            34.78843141500525,
                            31.336622682320776
                        ],
                        [
                            34.79414493381831,
                            31.355201806839506
                        ],
                        [
                            34.794207563998924,
                            31.371163684259038
                        ],
                        [
                            34.79296734448457,
                            31.375581921229493
                        ],
                        [
                            34.78922614608776,
                            31.390623411898762
                        ],
                        [
                            34.78827495215694,
                            31.408575008023988
                        ],
                        [
                            34.78636890354401,
                            31.42299930464733
                        ],
                        [
                            34.78632872060815,
                            31.42193697540446
                        ],
                        [
                            34.78629782738711,
                            31.422105913663092
                        ],
                        [
                            34.78628031267043,
                            31.421992838029546
                        ],
                        [
                            34.786190597348,
                            31.42158272671166
                        ],
                        [
                            34.78616186179376,
                            31.421905927160644
                        ],
                        [
                            34.784898002487694,
                            31.424062281290652
                        ],
                        [
                            34.773401914392345,
                            31.434990008872095
                        ],
                        [
                            34.76559077292494,
                            31.446642271859293
                        ],
                        [
                            34.77342394487551,
                            31.466799593779264
                        ],
                        [
                            34.77975547041099,
                            31.490976936335215
                        ],
                        [
                            34.78263023215561,
                            31.506823501958817
                        ],
                        [
                            34.792679583505226,
                            31.531054387260554
                        ],
                        [
                            34.80048435469499,
                            31.55136414506193
                        ],
                        [
                            34.80654617304027,
                            31.574946250797954
                        ],
                        [
                            34.813199698392914,
                            31.59725795152748
                        ],
                        [
                            34.81470088275501,
                            31.619577056013927
                        ],
                        [
                            34.809541545607935,
                            31.657018778594132
                        ],
                        [
                            34.80638283046715,
                            31.674769044083895
                        ],
                        [
                            34.81569739824674,
                            31.69878758883678
                        ],
                        [
                            34.82117079659263,
                            31.721351512377144
                        ],
                        [
                            34.822676820645704,
                            31.74065914102294
                        ],
                        [
                            34.82640625397537,
                            31.760366608188313
                        ],
                        [
                            34.83277797643619,
                            31.795874550345438
                        ],
                        [
                            34.84013377052116,
                            31.80349376296454
                        ],
                        [
                            34.85764369385628,
                            31.809682398342275
                        ],
                        [
                            34.87655834935133,
                            31.809001817509152
                        ],
                        [
                            34.89977578898199,
                            31.81087415167135
                        ],
                        [
                            34.91555026001387,
                            31.814641705371283
                        ],
                        [
                            34.92078885084598,
                            31.819327945165384
                        ],
                        [
                            34.94872743329605,
                            31.826006370096646
                        ],
                        [
                            34.96865364149424,
                            31.831077555261356
                        ],
                        [
                            34.977436258261335,
                            31.8348796713872
                        ],
                        [
                            34.988078880595516,
                            31.834460104995458
                        ],
                        [
                            34.99298330536876,
                            31.826840136006965
                        ],
                        [
                            35.00026754690508,
                            31.826135241324245
                        ],
                        [
                            35.005568854286416,
                            31.82592323790467
                        ],
                        [
                            35.01774054730234,
                            31.8215162498958
                        ],
                        [
                            35.02155196628523,
                            31.818288829834177
                        ],
                        [
                            35.02691352351924,
                            31.813341112120234
                        ],
                        [
                            35.03143746024918,
                            31.80979583988096
                        ],
                        [
                            35.03815953441021,
                            31.80619724694477
                        ],
                        [
                            35.045420139243944,
                            31.80474599411263
                        ],
                        [
                            35.05317138181679,
                            31.804966181835955
                        ],
                        [
                            35.06486714082564,
                            31.804141661709476
                        ],
                        [
                            35.07584180677903,
                            31.800281337346696
                        ],
                        [
                            35.08838316507283,
                            31.800341312456926
                        ],
                        [
                            35.10640165800501,
                            31.801455597207628
                        ],
                        [
                            35.12610124373968,
                            31.800681742893538
                        ],
                        [
                            35.14697634526269,
                            31.79722164231699
                        ],
                        [
                            35.16219627795028,
                            31.79329681701213
                        ],
                        [
                            35.171594439546396,
                            31.797658570113885
                        ],
                        [
                            35.187671634314036,
                            31.797928602082614
                        ],
                        [
                            35.188362774999014,
                            31.79653341639412
                        ],
                        [
                            35.1887256549307,
                            31.7961346714363
                        ],
                        [
                            35.19047234637513,
                            31.794962708212374
                        ],
                        [
                            35.19256271051755,
                            31.79461237466848
                        ],
                        [
                            35.19270785200568,
                            31.79363734891888
                        ],
                        [
                            35.193583458825614,
                            31.79312975679172
                        ],
                        [
                            35.194747974527196,
                            31.793406487587923
                        ],
                        [
                            35.196095096006964,
                            31.79164621288934
                        ],
                        [
                            35.196955729849186,
                            31.790773244292048
                        ],
                        [
                            35.1978874456438,
                            31.790160915552967
                        ],
                        [
                            35.198711620213004,
                            31.78987941300083
                        ],
                        [
                            35.19929508848269,
                            31.787229378909604
                        ],
                        [
                            35.19850752277577,
                            31.785375424789216
                        ],
                        [
                            35.19941639636708,
                            31.784687679081976
                        ],
                        [
                            35.199241688426945,
                            31.78231397727036
                        ],
                        [
                            35.198391385845284,
                            31.781402127099458
                        ],
                        [
                            35.201245278823734,
                            31.77605635342819
                        ]
                    ]
                ]
            },
            "geometry_name": "geom",
            "properties": {
                "Id": 1,
                "StartDateTime": "2013-06-23T01:49:57Z",
                "EndDateTime": "2013-06-23T03:23:32Z",
                "Length": 94.57
            }
        },
        {
            "type": "Feature",
            "id": "Trips.fid--3a106d6c_13f7fcd4d00_-7daa",
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [
                    [
                        [
                            35.20076873304309,
                            31.77473722793188
                        ],
                        [
                            35.204546738625524,
                            31.774719539653546
                        ],
                        [
                            35.208965075961345,
                            31.77618510615382
                        ],
                        [
                            35.208640148891355,
                            31.78202931583896
                        ],
                        [
                            35.20975512430616,
                            31.78246578222451
                        ],
                        [
                            35.21137792550667,
                            31.781691930524566
                        ],
                        [
                            35.213549710915274,
                            31.780813498217512
                        ],
                        [
                            35.21476900302076,
                            31.78168291327713
                        ],
                        [
                            35.21469230369844,
                            31.780946053301573
                        ],
                        [
                            35.21257227234834,
                            31.781205360240914
                        ],
                        [
                            35.20864390470302,
                            31.783054725633296
                        ],
                        [
                            35.20829895121278,
                            31.78302020473956
                        ],
                        [
                            35.203248270523765,
                            31.78222448311647
                        ],
                        [
                            35.19854766107952,
                            31.786334626536405
                        ],
                        [
                            35.19984531073088,
                            31.78751601702663
                        ],
                        [
                            35.20014413481394,
                            31.788041155898213
                        ],
                        [
                            35.19725069211512,
                            31.790485693913038
                        ],
                        [
                            35.19110776321938,
                            31.795067815242238
                        ],
                        [
                            35.18161040176461,
                            31.801656830805612
                        ],
                        [
                            35.1687011405429,
                            31.79567693197168
                        ],
                        [
                            35.159244819963,
                            31.795794581845737
                        ],
                        [
                            35.11942475024883,
                            31.798775201079778
                        ],
                        [
                            35.08039358590231,
                            31.800856179659302
                        ],
                        [
                            35.063209196166355,
                            31.805088407881986
                        ],
                        [
                            35.04805678839844,
                            31.804031639631894
                        ],
                        [
                            35.032927570632985,
                            31.80847877245744
                        ],
                        [
                            35.02092427914747,
                            31.81877639146
                        ],
                        [
                            34.99012978758847,
                            31.829259174717787
                        ],
                        [
                            34.98804795187599,
                            31.839040689444193
                        ],
                        [
                            34.977978798901404,
                            31.835101996624207
                        ],
                        [
                            34.9498888706375,
                            31.82596783070529
                        ],
                        [
                            34.92736197558343,
                            31.823111392882268
                        ],
                        [
                            34.91695636557349,
                            31.815625240675068
                        ],
                        [
                            34.903688415937665,
                            31.811906632219383
                        ],
                        [
                            34.88296941318597,
                            31.810547012219338
                        ],
                        [
                            34.86002652341891,
                            31.81005225691859
                        ],
                        [
                            34.8400240232646,
                            31.803567891209664
                        ],
                        [
                            34.83015973660191,
                            31.79428193887949
                        ],
                        [
                            34.832461959577664,
                            31.781116935605244
                        ],
                        [
                            34.82711462326763,
                            31.762582638502867
                        ],
                        [
                            34.82278272110514,
                            31.743736272787874
                        ],
                        [
                            34.82054030348634,
                            31.720919484741522
                        ],
                        [
                            34.81420911419586,
                            31.695453086060326
                        ],
                        [
                            34.80627534157327,
                            31.675224452093136
                        ],
                        [
                            34.81001464412418,
                            31.653220249705704
                        ],
                        [
                            34.814807584458386,
                            31.623882775804965
                        ],
                        [
                            34.81329122844819,
                            31.60270186644005
                        ],
                        [
                            34.8080051217612,
                            31.582810947810554
                        ],
                        [
                            34.80303812730152,
                            31.560726326313713
                        ],
                        [
                            34.797198877391246,
                            31.54151166321618
                        ],
                        [
                            34.786754234512,
                            31.52322945275979
                        ],
                        [
                            34.78295267962934,
                            31.504485052320216
                        ],
                        [
                            34.77614342612191,
                            31.48399237576942
                        ],
                        [
                            34.77342018413583,
                            31.467251749582854
                        ],
                        [
                            34.76645583238203,
                            31.450437751509558
                        ],
                        [
                            34.76590528960303,
                            31.442231443933725
                        ],
                        [
                            34.78525964273001,
                            31.422175881020067
                        ],
                        [
                            34.78907352850801,
                            31.401593669076053
                        ],
                        [
                            34.79129725926219,
                            31.38162192285955
                        ],
                        [
                            34.795717357038875,
                            31.365271563573277
                        ],
                        [
                            34.792079987685526,
                            31.350441990935515
                        ],
                        [
                            34.786453724639145,
                            31.329829087974357
                        ],
                        [
                            34.78839683025119,
                            31.309436171497104
                        ],
                        [
                            34.79081076415792,
                            31.289914211434404
                        ],
                        [
                            34.7976451665849,
                            31.28794303366183
                        ],
                        [
                            34.80366544178834,
                            31.286754464613146
                        ]
                    ]
                ]
            },
            "geometry_name": "geom",
            "properties": {
                "Id": 2,
                "StartDateTime": "2013-06-23T04:46:14Z",
                "EndDateTime": "2013-06-23T06:03:47Z",
                "Length": 102.14
            }
        }
    ],
    "crs": {
        "type": "EPSG",
        "properties": {
            "code": "4326"
        }
    }
};

var jsonString = JSON.stringify(json);
vector.addFeatures(jsonFormat.read(jsonString));
  • Thank you , but this is not the case. When I load WMS layer which is SQL view , the items I get are the columns in the view (including the geometry item). How I convert that to feature. – Alophind Jun 28 '13 at 17:19
  • It is not clear why proposed solution doesnt't fit to your case. When you get GetFeatureInfo response from your WMS server (as I can see in GeoJSON format), you should parse it using OpenLayers.Format.GeoJSON.read method (convert to feature) and add features to vector layer. – drnextgis Jun 29 '13 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.