Package org.geoforge.plggazetteers.gov.nasa.worldwind.poi

Source Code of org.geoforge.plggazetteers.gov.nasa.worldwind.poi.GisgraphyGazetteer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.geoforge.plggazetteers.gov.nasa.worldwind.poi;

import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.exception.NoItemException;
import gov.nasa.worldwind.exception.ServiceException;
import gov.nasa.worldwind.exception.WWRuntimeException;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.poi.BasicPointOfInterest;
import gov.nasa.worldwind.poi.Gazetteer;
import gov.nasa.worldwind.poi.POIUtils;
import gov.nasa.worldwind.poi.PointOfInterest;
import java.io.ByteArrayInputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/*
* from http://forum.worldwindcentral.com/showthread.php?25731-Another-geocoder-software-instead-of-YahooGazetteer
*/

/**
* A gazetteer that uses Gisgraphy's geocoding service to find locations for requested places.
*
* @author tag
* @version $Id: GisgraphyGazetteer.java 1 2011-07-16 23:22:47Z dcollins $
*/
public class GisgraphyGazetteer implements Gazetteer
{
    protected static final String GEOCODE_SERVICE = "http://services.gisgraphy.com/fulltext/fulltextsearch";

    @Override
    public List<PointOfInterest> findPlaces(String lookupString) throws NoItemException, ServiceException
    {
        if (lookupString == null || lookupString.length() < 1)
            return null;

        String urlString ;

                try
                {
                        urlString = GEOCODE_SERVICE + "?" + "q=" + URLEncoder.encode( lookupString )+
                                //"&lat=" +
                                //"&lng=" +
                                "&__multiselect_placetype=" +
                                "&allwordsrequired=true" +
                                "&country=" +
                                "&spellchecking=true" +
                                "&__checkbox_spellchecking=true" +
                                "&lang=" +
                                "&format=XML" +
                                "&style=MEDIUM" +
                                "&__checkbox_indent=true" +
                                "&from=1&to=10" ;
        }
               
        catch( Exception e )
        {
                e.printStackTrace();
                return null ;
        }

        String locationString = POIUtils.callService( urlString );

        if (locationString == null || locationString.length() < 1)
            return null;

        return this.parseLocationString( locationString );
    }

    protected ArrayList<PointOfInterest> parseLocationString(String locationString) throws WWRuntimeException
    {
        try
        {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setNamespaceAware( false );
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new ByteArrayInputStream(locationString.getBytes("UTF-8")));

            XPathFactory xpFactory = XPathFactory.newInstance();
            XPath xpath = xpFactory.newXPath();

            NodeList resultNodes = (NodeList) xpath.evaluate(
                //"/ResultSet/Result", doc, XPathConstants.NODESET
                                "/response/result/doc", doc, XPathConstants.NODESET
                                );

            ArrayList<PointOfInterest> lstPois = new ArrayList<PointOfInterest>(resultNodes.getLength());

            for (int i = 0; i < resultNodes.getLength(); i++)
            {
                Node node = resultNodes.item(i);

                NodeList children = node.getChildNodes();

                String lat = "" ;
                String lon = "" ;
                String pla = "" ;

                for ( int j = 0; j< children.getLength(); j++ )
                {
                        Node nj = children.item(j);

                        NamedNodeMap nj_atts = nj.getAttributes();

                        Node nj_att = nj.getAttributes().item(0);

                        String att_value = nj_att.getNodeValue();

                        if ( att_value.equals("lat") == false &&
                                att_value.equals("lng") == false &&
                                att_value.equals("fully_qualified_name") == false )
                                continue ;

                        NodeList nj_children = nj.getChildNodes();

                        Node nk = nj_children.item(0);

                        if ( att_value.equals("lat") && nk.getNodeName().equals("#text" ) )
                                lat = nk.getTextContent() ;

                        if ( att_value.equals("lng") && nk.getNodeName().equals("#text" ) )
                                lon = nk.getTextContent() ;

                        if ( att_value.equals("fully_qualified_name") && nk.getNodeName().equals("#text" ) )
                                pla = nk.getTextContent() ;
                }

                if ( lat.equals("") == false && lon.equals("") == false && pla.equals("") == false )
                {
                        LatLon latlon = LatLon.fromDegrees(Double.parseDouble(lat), Double.parseDouble(lon));
                        PointOfInterest loc = new BasicPointOfInterest(latlon);
                        loc.setValue(AVKey.DISPLAY_NAME, pla );
                        lstPois.add(loc);
                                }
            }

            return lstPois;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null ;
        }
    }
TOP

Related Classes of org.geoforge.plggazetteers.gov.nasa.worldwind.poi.GisgraphyGazetteer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.