[OpenLayers-Users] Using Cross Domain JSON to get around a proxy issue

Phil Scadden p.scadden at gns.cri.nz
Tue Sep 20 00:15:07 EDT 2011


yes, I've done it with eg.

Instead of
                     wfsProtocol = new OpenLayers.Protocol.WFS.v1_1_0({
                         url: myurl,
                         geometryName: "SHAPE",
                         featurePrefix: myfeaturePrefix,
                         featureType: myfeatureType,
                         srsName: myproj
                     });

I use

                     wfsProtocol = new OpenLayers.Protocol.Script({
                         url: myurl,
                         callbackKey: "format_options",
                         callbackPrefix: "callback:",
                         params: {
                             service: "WFS",
                             version: "1.1.0",
                             srsName: myproj
                             request: "GetFeature",
                             featurePrefix: myfeaturePrefix,
                             typeName: myfeatureType,
                             outputFormat: "json"
                         },
                         filterToParams: function (filter, params) {
                             // example to demonstrate BBOX serialization
                             delete params.cql_filter;
                             if (filter.type === 
OpenLayers.Filter.Spatial.BBOX) {
                                 params.bbox = filter.value.toArray();
                                 if (filter.projection) {
                                     
params.bbox.push(filter.projection.getCode());
                                 }
                             } else if (filter.type === 
OpenLayers.Filter.Spatial.DWITHIN) {
                                 var geom = filter.value.clone();
                                 transformGeometry(geom);
                                 if (geom.x >= -180 && geom.x <= 360 && 
geom.y >= -180 && geom.y <= 360) { // is this a lat long?
                                     var xx = geom.x;
                                     geom.x = geom.y;
                                     geom.y = xx;
                                 }
                                 params.cql_filter = 'DWITHIN(SHAPE,' + 
geom.toString() + ',' + filter.distance + ',meters)';
                             } else if (filter.type === 
OpenLayers.Filter.Spatial.INTERSECTS) {
                                 var geom = filter.value;
                                 transformGeometry(geom);
                                 geom.components[0].swapXY();
                                 params.cql_filter = 'INTERSECTS(SHAPE,' 
+ geom.toString() + ')';
                             } else if 
(OpenLayers.Format.QueryStringFilter) {
                                 var format = new 
OpenLayers.Format.QueryStringFilter({
                                     srsInBBOX: this.srsInBBOX
                                 });
                                 params = format.writecql(filter, 
params, this.options.url.indexOf('server21') > 0);
                             }
                             return params;
                         }
                     });

The complicated bit the filterToParams. If you dont have any filter, 
then no problem. If you do, they you need to convert the filter to CQL. 
The code above handles spatial filters. I havent attempted others. Gets 
complicated fast. CQL filters dont respect SRS (at least on geoserver). 
You have to make sure that any geometry is transformed to native SRS 
(that's what my application function transformGeometry is doing). You 
also have switch x,y if it is lat/long.


Notice: This email and any attachments are confidential. If received in error please destroy and immediately notify us. Do not copy or disclose the contents.



More information about the Users mailing list