Re: [OpenMap Users] How do I dermine which country a lat/lon pt belongs to?

From: Scott Bortman <sbortman@imagelinks.com>
Date: Thu Jan 29 2004 - 12:53:26 EST

Greetings all,

I think I have the answer to my own question. I thought I'd post what
worked
for me in case somebody else needs it:

package mapserver;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.io.FormatException;
import com.bbn.openmap.layer.shape.ESRIRecord;
import com.bbn.openmap.layer.shape.areas.AreaShapeLayer;
import com.bbn.openmap.omGraphics.OMGeometry;
import com.bbn.openmap.omGraphics.OMGeometryList;
import com.bbn.openmap.proj.Projection;
import com.bbn.openmap.proj.ProjectionFactory;
import com.bbn.openmap.util.Debug;
import com.bbn.openmap.util.PropUtils;

/*
 * Created on Jan 28, 2004
 *
 * To change the template for this generated file go to Window -
Preferences -
 * Java - Code Generation - Code and Comments
 */

/**
 * @author Scott Bortman
 *
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class CountryCodeLookupUtil extends AreaShapeLayer
{
 
  public String lookupCountryCode(
  String projectionName,
  float latitude,
  float longitude,
  float scale,
  int width,
  int height,
  int x,
  int y)
  {
    Projection projection =
    ProjectionFactory.makeProjection(
    ProjectionFactory.getProjType(projectionName),
    latitude,
    longitude,
    scale,
    width,
    height);
   
    setProjection(projection);
    computeGraphics();
   
    LatLonPoint lx = projection.getUpperLeft();
    LatLonPoint ly = projection.getLowerRight();
    Point center = projection.forward(latitude, longitude);
    String name = null;
   
    try
    {
      ESRIRecord[] records =
      spatialIndex.locateRecords(
      Math.min(lx.getLongitude(), ly.getLongitude()),
      Math.min(lx.getLatitude(), ly.getLatitude()),
      Math.max(lx.getLongitude(), ly.getLongitude()),
      Math.max(lx.getLatitude(), ly.getLatitude()));
     
      for (int i = 0; i < records.length; i++)
      {
        OMGeometryList glist = new OMGeometryList();
        OMGeometry geom = records[i].addOMGeometry(glist);
        Integer id = new Integer(records[i].recordNumber);
       
        for (int j = 0; j < glist.size(); j++)
        {
          OMGeometry g = glist.getAt(j);
         
          g.regenerate(projection);
         
          if (g.contains(x, y))
          {
            name = areas.getName(id);
            break;
          }
        }
      }
    }
    catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (FormatException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
    return name;
  }
 
  public void init(String prefix, Properties properties)
  {
    areas.initialize(prefix, properties);
  }
 
  public static void main(String[] args)
  {
    Debug.init();
   
    // Init params
    String configDir = "c:\\openmap\\openmap-4.5.4\\share";
    String configFile = "openmap.properties";
    String prefix = "tz";
   
    CountryCodeLookupUtil util = new CountryCodeLookupUtil();
    Properties properties = new Properties();
   
    if (PropUtils.loadProperties(properties, configDir, configFile))
    {
      util.setProperties(prefix, properties);
     
      if ( Debug.debugging("lookup"))
      {
        for (Iterator i = properties.keySet().iterator(); i.hasNext();)
        {
          String key = i.next().toString();
         
          if (key.startsWith(prefix))
            Debug.output(key + "=" + properties.getProperty(key));
        }
      }
     
      // Runtime Params
      String projectionName = "LLXY";
      boolean zoomedOut = false;
      float latitude = (zoomedOut) ? 0.0f : 27.288322f;
      float longitude = (zoomedOut) ? 0.0f : -81.44919f;
      float scale = (zoomedOut) ? Float.MAX_VALUE : 9375000.0f;
      int width = 512;
      int height = 512;
     
      // Results
      String countryCode =
      util.lookupCountryCode(
      projectionName,
      latitude,
      longitude,
      scale,
      width,
      height,
      width/2,
      height/2);
     
      System.out.println(countryCode);
     
    }
    else
    {
      System.err.println(
      "Error: Cannot find config file: "
      + configDir
      + File.separator
      + configFile);
    }
  }
}

Robert Najlis wrote:

> Scottie,
>
> I have done something like this, using a shp, not a dbf file (I did
> not have a dbf file). Let me try to dig up the code. (it might take a
> bit, but I will get back to you).
>
> Robert
>
>
>
>
>
>
> On Jan 29, 2004, at 9:36 AM, Scott Bortman wrote:
>
>> Hello,
>>
>> I'm stuck on a problem and maybe somebody has run accross it before.
>>
>> I wrote a layer that extends AreaShapeLayer to do custom shading of
>> countries based upon a database query. This works really well. I saw
>> the example on the list archives about time zone shading and did
>> something
>> similar.
>>
>> Now I want want to be able specify a lat/lon (like if a user clicks
>> the map)
>> and determine which country that polygon belongs to. Any idea how to do
>> the reverse lookup?
>>
>> Thanks,
>> --scottie
>>
>> --
>> [To unsubscribe to this list send an email to "majdart@bbn.com"
>> with the following text in the BODY of the message "unsubscribe
>> openmap-users"]
>
>
>

--
[To unsubscribe to this list send an email to "majdart@bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Thu Jan 29 12:51:42 2004

This archive was generated by hypermail 2.1.8 : Thu May 12 2005 - 07:18:37 EDT