[OpenLayers-Users] Re: Get Features from a WMS Layer

IvanBell Ivan.Bell at CCA.com
Wed May 25 15:09:05 EDT 2011


I was finally able to get back to this.  Thank you ever so much, Xurxo.  That
was precisely what I needed.  Although the issue function did not work for
me, I was successfully able to use the GET.


For the other readers, here is my solution (short and sweet):




         /**
          * Return the list of feature ids for the specified layer.
          *
          * @param p_strLayerName the specified layer for which to return
the
          *        array of features.
          */
         function getFeatureIds( p_strLayerName ) {
            //
            // Get the features for the specified layer.
            //
            var oXml = OpenLayers.Request.GET({
                                                url:
"http://localhost:8081/geoserver/wfs?service=wfs&version=1.1.0&request=GetFeature&typeName="
+ p_strLayerName,
                                                async: false
                                             });
            var strXml = oXml.text;
            var oXmlDoc = null;

            //
            // Response may have XML in either text or responseText.
            //
            if (
                 ( strXml == null ) ||
                 ( strXml.length == 0 )
               )
            {
               strXml = oXml.responseText;
            }

            //
            // Parse XML for Mozilla, Safari, Opera, et. al.
            //
            if ( window.DOMParser ) {
               var oXmlParser = new DOMParser();
               oXmlDoc = oXmlParser.parseFromString( strXml, "text/xml" );

            //
            // Parse for Internet Explorer
            //
            } else {
               oXmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
               oXmlDoc.async = "false";
               oXmlDoc.loadXML( strXml );
            }

            //
            // This code parses XML document to get the features by tag
name.
            //
            var arrFeatures = oXmlDoc.getElementsByTagName( p_strLayerName
);
            var astrFeatureIds = [];

            for ( var i = 0; i < arrFeatures.length; i++ ) {
               astrFeatureIds.push( arrFeatures[ i ].attributes[ 0
].nodeValue );
            }

            return astrFeatureIds;
         }



The async parameter was necessary since I did not use the optional callback
functions and did not want the code to continue asynchronously.  When used
the callback methods, I could not grab the list of features to return. 
I.e., Javascript ran asynchronously and there was no way to sleep or wait or
block on the callback function, so I was unable to return the list of
feature ids (was returning null).


Thanks again for all your help.


Sincerely,

Ivan Bell



-----
Up the Irons!!!
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Get-Features-from-a-WMS-Layer-tp6396039p6404073.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110525/68412f71/attachment-0001.html


More information about the Users mailing list