metteo / javageomodel

Automatically exported from code.google.com/p/javageomodel
0 stars 0 forks source link

Getting Started with Geomodel in java #13

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
geocell-0.0.3-SNAPSHOT.jar 
on Windows 7

Please provide any additional information below.
I am new to Google App Engine. I would like to use Geomodel to be able to store 
spatial data and retrieve them using boundingbox and proximity queries. I read 
all of the forums documentations for Geomodel in java. However, I still have 
problem using Geomodel even in storing data. 
I really appreciate if someone gives me step-by-step instruction on how to 
create a simple GAE application for storing & querying spatial data. 

Thanks.

Original issue reported on code.google.com by Mahsa.Gh...@gmail.com on 12 Jul 2010 at 5:07

GoogleCodeExporter commented 9 years ago
I am using Eclipse, and here is what I did: 

1) Created a Web application project enabling only Google App Engine (1.3.4)(as 
Google SDK)

2) Created a simple jsp file in "war" folder to get latitude & longitude from 
user

3) Created two java classes, UseGeocell and ObjectToSave. UseGeocell class is 
similar to the code in 
http://code.google.com/p/javageomodel/source/browse/trunk/geocell/src/test/java/
com/beoui/utils/HowToUseGeocell.java 
ObjectToSave is exactly 
http://code.google.com/p/javageomodel/source/browse/trunk/geocell/src/test/java/
com/beoui/utils/ObjectToSave.java

4)In my Servlet I defined an object of type UseGeocell and called 
testHowToSaveGeocellsInDatabase function in order to save latitude & longitude 
that are obtained from the input

Here is my code:

My UseGeocell class:

package edu.pitt.Test8_Geomodel_GAE;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    import javax.jdo.PersistenceManager;

    import junit.framework.Assert;
    import junit.framework.TestCase;

    import com.beoui.geocell.GeocellManager;
    import com.beoui.geocell.model.BoundingBox;
    import com.beoui.geocell.model.CostFunction;
    import com.beoui.geocell.model.GeocellQuery;
    import com.beoui.geocell.model.Point;

    /**
     * Unit test also used to explain how to use Geocell class.
     *
     * @author Alexandre Gellibert <alexandre.gellibert@gmail.com>
     *
     */
    public class UseGeocell extends TestCase {

        private final Logger log = Logger.getLogger("com.beoui.utils");
        public ObjectToSave obj;

        /**
         * First step is to save your entities.
         * In database, you don't save only latitude and longitude of your point but also geocells around this point.
         */
        public void testHowToSaveGeocellsInDatabase(double lat, double lon) {
            // Incoming data: latitude and longitude (Bordeaux for instance)
            //double lat = 44.838611;
            //double lon = -0.578333;

            // Transform it to a point
            Point p = new Point(lat, lon);

            // Generates the list of GeoCells
            List<String> cells = GeocellManager.generateGeoCell(p);

            // Save your instance
            obj = new ObjectToSave();
            obj.setLatitude(lat);
            obj.setLongitude(lon);
            obj.setGeocells(cells);

            //objDao.save(obj);

            // Just checking that cells are not empty
            Assert.assertTrue(cells.size() > 0);

            // Show in the log what cells are going to be saved
            log.log(Level.INFO, "Geocells to be saved for Point("+lat+","+lon+") are: "+cells);
        }

        /**
         * Second step, now entities are in database, we can query on them.
         * Here is the example of a bounding box query.
         *
         */
        public void testHowToQueryOnABoundingBox(double latSW, double lonSW, double latNE, double lonNE) {
            // Incoming data: latitude and longitude of south-west and north-east points (around Bordeaux for instance =) )
            //double latSW = 44.8;
            //double lonSW = -0.6;

           // double latNE = 44.9;
           // double lonNE = -0.7;

            // Transform this to a bounding box
            BoundingBox bb = new BoundingBox(latNE, lonNE, latSW, lonSW);

            // Calculate the geocells list to be used in the queries (optimize list of cells that complete the given bounding box)
            List<String> cells = GeocellManager.bestBboxSearchCells(bb, null);

            // OR if you want to use a custom "cost function"
            List<String> cells2 = GeocellManager.bestBboxSearchCells(bb, new CostFunction() {

                @Override
                public double defaultCostFunction(int numCells, int resolution) {
                    if(numCells > 100) {
                        return Double.MAX_VALUE;
                    } else {
                        return 0;
                    }
                }
            });

            // Use this in a query
            // In Google App Engine, you'll have something like below. In hibernate (or whatever else), it might be a little bit different.
//                String queryString = "select from ObjectToSave where 
geocellsParameter.contains(geocells)";
//                Query query = pm.newQuery(query);
//            query.declareParameters("String geocellsParameter");
//            query.declareParameters("String geocellsP");
//            List<ObjectToSave> objects = (List<ObjectToSave>) 
query.execute(cells);

            // Just checking that cells are not empty
            Assert.assertTrue(cells.size() > 0);
            Assert.assertTrue(cells2.size() > 0);

            // Show in the log what cells shoud be used in the query
            log.log(Level.INFO, "Geocells to use in query for PointSW("+latSW+","+lonSW+") ; PointNE("+latNE+","+lonNE+") are: "+cells);
        }

        /**
         * To test proximity search, you have to give your base query and it will be enhanced with geocells restrictions.
         *
         */
        // TODO configure persistent manager to run a real test
        public void testHowToQueryWithProximitySearch(double lat, double lon) {
            Point center = new Point(lat, lon);
            PersistenceManager pm = null;// here put your persistent manager
            List<Object> params = new ArrayList<Object>();
            params.add("John");
            GeocellQuery baseQuery = new GeocellQuery("lastName == lastNameParam", "String lastNameParam", params);

            List<ObjectToSave> objects = null;
            try {
                objects = GeocellManager.proximityFetch(center, 40, 0, ObjectToSave.class, baseQuery, pm);
                Assert.assertTrue(objects.size() > 0);
            } catch (Exception e) {
                // We catch exception here because we have not configured the PersistentManager (and so the queries won't work)
            }
        }

    }

My servlet:

package edu.pitt.Test8_Geomodel_GAE;

import java.io.IOException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class Test8_Geomodel_GAEServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        doPost(req, resp);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
        String lat = req.getParameter("txtlatitude");
        String lon = req.getParameter("txtlongitude");

        double latitude = Double.parseDouble(lat);
        double longitude = Double.parseDouble(lon);

        UseGeocell gc = new UseGeocell();

        gc.testHowToSaveGeocellsInDatabase(latitude, longitude);

        resp.sendRedirect("/main.jsp");

    }
}

My jsp file:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<%@ page import="javax.jdo.PersistenceManager" %>

<%@ page import= "edu.pitt.Test8_Geomodel_GAE.Test8_Geomodel_GAEServlet" %>
<%@ page import= "edu.pitt.Test8_Geomodel_GAE.UseGeocell" %>

<html>
    <head><title>Geomodel GAE Project_helloo2</title></head>
    <body bgcolor="#ffffff" background="background.gif">

        <table border="0" width="700">
            <tr>
                <td width="150">   </td>
                <td width="50"> 
                    <h1>Enter a point:</h1>
                </td>
            </tr>
            <tr>
                <td width="150">   </td>
                <td width="550">

                    <form action="/test8_geomodel_gae" method="post">
                    <br>Latitude:
                        <input type="text" name="txtlatitude" size="25">
                    <br>Longitude:
                        <input type="text" name="txtlongitude" size="25">
                    <br>
                        <input type="submit" value="Submit">
                    </form>
                </td>
            </tr>

        </table>
  </body>
</html>

My web.xml file:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <servlet>
        <servlet-name>Test8_Geomodel_GAE</servlet-name>
        <servlet-class>edu.pitt.Test8_Geomodel_GAE.Test8_Geomodel_GAEServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Test8_Geomodel_GAE</servlet-name>
        <url-pattern>/test8_geomodel_gae</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>main.jsp</welcome-file>
    </welcome-file-list>
</web-app>

The code is compiled and run successfully. However, when in the browser, when I 
submit latitude & longitude I receive an error as follow:

HTTP ERROR 500

Problem accessing /test8_geomodel_gae. Reason:

    com/beoui/geocell/model/Point

Caused by:

java.lang.NoClassDefFoundError: com/beoui/geocell/model/Point
    at edu.pitt.Test8_Geomodel_GAE.Test8_Geomodel_GAEServlet.doPost(Test8_Geomodel_GAEServlet.java:45)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:51)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:70)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:349)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

I am not sure what I am doing wrong. I really appreciate it if someone help me 
with this issue. 

Original comment by Mahsa.Gh...@gmail.com on 13 Jul 2010 at 8:08

GoogleCodeExporter commented 9 years ago
It's been solved by adding the external JAR files to the class path

Original comment by Mahsa.Gh...@gmail.com on 19 Jul 2010 at 7:44

GoogleCodeExporter commented 9 years ago
Just a comment - the project has Maven pom.xml, so Eclipse project can be 
generated with mvn eclipse:eclipse

Original comment by gmi...@gmail.com on 24 Sep 2010 at 11:01

GoogleCodeExporter commented 9 years ago

Original comment by alexandr...@gmail.com on 10 Nov 2010 at 7:32

GoogleCodeExporter commented 9 years ago
Main page (instructions) has been updated. You can use the project by declaring 
the repository and add the dependency in your pom.

Original comment by alexandr...@gmail.com on 11 Nov 2010 at 6:33

GoogleCodeExporter commented 9 years ago
I installed the svn wagon for mvn, but now I'm getting prompted for a pwd to 
pull the jar from the svn repository:

Authentication realm: <https://javageomodel.googlecode.com:443> Google Code 
Subversion Repository
Username:

Original comment by orton.n...@gmail.com on 11 Nov 2010 at 11:15

GoogleCodeExporter commented 9 years ago
Please use this instead, notice the <url>:
<repository>
  <id>javageomodel-repo</id>
  <name>GeocellJava Repository</name>
  <url>http://javageomodel.googlecode.com/svn/repository</url>
</repository>

Original comment by edgar.da...@gmail.com on 11 Nov 2010 at 11:50