1

I'm trying to figure out the best way on Android to figure out if a point in region as defined in a shapefile. For instance, I might want to know what country I'm in using the Geo Common's Shapefile file for Countries. Is there an existing Android library that can manage such tasks with Shapefiles, or an equivalent substitute (KML, for instance)? A few other points:

  1. I might be using a lot of regions. For instance, I might want to know what county I'm in, along with state and country. Thus, there is potential for overlap of regions.
  2. Speed is desirable, if possible. I might, for instance, figure out which country I'm in, then state, then county, and only look at the applicable states for a country, and counties for a state.
  3. Compactness is very important. At least one of the data files I want to work with is 47 Mb as a KML, Shapefiles seems like a reasonable size, assuming I can find a decent library for it.
  • You still haven't told us what kind of client you are using or creating. If you can use HTML5, d3.js with TopoJSON and a custom point-in-polygon function might be a good option. – blah238 Oct 28 '13 at 22:52
  • There is mention of a point-in-polygon function used in this d3js plug-in that might be worth a look at: gtb104.wordpress.com/2012/09/08/polybrush-d3-js-plugin – blah238 Oct 28 '13 at 22:54
  • The client will be a native android app in java. All that will be output is the name of the region that I'm in, ie, what country I'm in. – PearsonArtPhoto Oct 28 '13 at 22:56
  • Well there is the JTS Topology Suite, but that seems like it might be overkill. It also only works with WKT, WKB and GML, none of which are particularly space-efficient. – blah238 Oct 28 '13 at 23:06
3

I will suggest that you use SpatiaLite in your Android Application to store the Countries Data.

There is a loader with a GUI available for loading your shapefile into the SpatiaLite Database.

Once your data in inside the database, you can do spatial queries directly from your Android Application. The Spatialite Tutorial has some examples on how you can query the database.

The following code will show how you can do this:

//AppBase is a folder
File SpatialDBFile=new File(AppBase, "gdb.sqlite");
if(SpatialDBFile.exists())
{
   jsqlite.Database db = new jsqlite.Database();
  db.open(SpatialDBFile.getAbsolutePath(),
                jsqlite.Constants.SQLITE_OPEN_READWRITE);


String query="select objectId, name, AsText(Geometry) from countries where 
 Within(MakePoint("+longitude.toString()+ ", "+ latitude.toString() +"), Geometry)=1;";

    try{
        Stmt statement=db.prepare(query);
        while(statement.step())
        {
            String name=statement.column_string(1); 
            //do whatever you need to do with this name
        }
    }catch (Exception e){
        //handle the exception
    }
    db.close();
}
  • This looks like it's EXACTLY what I was looking for. Just have to figure out how to convert my data files in to the right format, and I should be good to go. Thanks! – PearsonArtPhoto Oct 29 '13 at 13:20
  • @PearsonArtPhoto: If your data is in shapefile format, you can easily load it into the spatialite database. – Devdatta Tengshe Oct 29 '13 at 13:45
  • I haven't looked in to those specifics yet, but I can see it looks like it'll be pretty easy to load in. Just not in a place where I can figure out how to do it yet. – PearsonArtPhoto Oct 29 '13 at 13:56
2

Seems like you need:

  1. point-in-polygon feature for android. For this you may want to find good client library for mapping. This post has some good ideas: Is there an comparison of the various mapping libraries for Android? If you want to write your own point-in-poly, there are a bunch of examples out there including: https://stackoverflow.com/questions/11716268/point-in-polygon-algorithm

  2. shapefile to android interface. You may find that shapefile is not the most convinent way to store your data. I would recommend looking at geojson as a storage format instead of shapefile because its simpler file structure, and a standard web protocol. https://stackoverflow.com/questions/18445191/android-google-map-polygon-click-event

Are you using any mapping client library for Android?

  • Also try TopoJSON if you are concerned about file sizes. It should work very well with contiguous polygons. – blah238 Oct 28 '13 at 22:38
  • Looking at the libraries referenced, they all seem to be focused on the display of maps, and not on the loading of them to do other things. I don't care about the web protocol as much, as the tool I'm working on needs to run without internet access. – PearsonArtPhoto Oct 28 '13 at 22:52
  • I suggest a flavor of JSON not related to internet access, rather for interop. good JSON parser is better investment than good shapefile parse IMHO. and easier get right. you also can store data a single file and without shapefile field name character length issues and data type. – bcollins Oct 31 '13 at 18:19

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.