[OpenLayers-Users] Query Feature by Attribute and Zoom

Gregory Roby groby at tce.coop
Tue Apr 15 16:00:52 EDT 2008


Thanks to Guillaume and Brad for their suggestions to get me on the right
path, I have the feature working as I wanted now.

I decided to use GeoDjango because I will be doing some other tasks with it
in the future.  Getting the transform function in GeoDjango took quite a bit
of work (I ended up hacking the django/contrib/gis/gdal/libgdal.py file and
setting the lib_name manually to the location of my libgdal.so file).  All
of the other instructions on the GeoDjango Wiki worked for me.

After getting everything installed and working, actually adding the zoom by
attribute feature was the easy part.



If a code example would be useful for anyone, here is how my Views file
looks:

# Create your views here.
from django.template import Context, loader
from django.http import HttpResponse
from tcecmapviewer.basicviewer.models import *
from django import newforms as forms

def index(request):
    if request.method == 'POST':
        mapForm = MapForm(request.POST)
    else:
        mapForm = MapForm()
 
myModel=MyModel.objects.all().filter(tagnumber=mapForm.data['TagNumber'])
    OpenLayersZoom=GetOpenLayersZoom(myModel)
    t =
loader.get_template('/var/django/tcecmapviewer/templates/index.html')
    c = Context({'OpenLayersZoom': OpenLayersZoom, 'mapForm': mapForm})
    return HttpResponse(t.render(c))


class MapForm(forms.Form):
    TagNumber=forms.CharField(max_length = 20)

def GetOpenLayersZoom(Object):
    if(Object):
        OneObject=GetOne(Object)
        point=OneObject.Point.transform(4326, clone=True)
        return "map.setCenter(new OpenLayers.LonLat(" + str(point.x) + "," +
str(point.y) + "), 14);" #Zoom into the point Object
    else:
        return "map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);"

def GetOne(ListOrObject):
    """Helper Function, return the 1st object in a list or the object"""
    try:
        return ListOrObject[0]
    except:
        return ListOrObject





My index.html (Template) file looks like this [modified slightly]:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <style type="text/css">
        #map {
            width: 800px;
            height: 600px;
            border: 1px solid black;
        }
    </style>
    <script src="/mapserver/lib/OpenLayers.js"></script>
    <script type="text/javascript">
        var lon = -80.9;
        var lat = 33.7;
        var zoom = 8;
        var map, LayerNetwork, scalebar;
	var options = {maxResolution: 0.17578125, numZoomLevels: 18,
maxExtent: new OpenLayers.Bounds(-83, 33, -79, 35),
		'tileSize': new OpenLayers.Size(512,512)};
        function init(){
            map = new OpenLayers.Map('map', options);
            LayerNetwork = new OpenLayers.Layer.WMS("Network", 
 
"/cgi-bin/mapserv?MAP=/var/www/mapserver/tcec/tcec.map",{Layers:"Line"},
		     {'buffer':0});
            map.addLayer(LayerNetwork);
	    {{ OpenLayersZoom }}     
        }
    </script>
  </head>
  <body onload="init()">
	<h2>Web Mapping Application Preview</h2>
 <table>
<tr><td><div id="map"></div></td><td><img
src="/cgi-bin/mapserv?mode=legend&map=/var/www/mapserver/tcec/tcec.map"></td
></tr>
</table>

<form method="post" action="">
	<table>{{ mapForm }} </table>
	<input type="submit" />
</form>
</html>


Thanks again for the help.

Greg












-----Original Message-----
From: Guillaume Sueur [mailto:guillaume.sueur at neogeo-online.net] 
Sent: Thursday, April 10, 2008 5:06 PM
To: Gregory Roby
Cc: users at openlayers.org
Subject: Re: [OpenLayers-Users] Query Feature by Attribute and Zoom

Maybe GeoDjango is a little bit oversized for this kind of thing ;-)
Basically, you just need to pull the extent of where you want to zoom to 
on the client side and set the map extent with it.
By the way, you can even probably grab it from Geonames 
(http://www.geonames.org/export/web-services.html#countryInfo)

Cheers,


Guillaume

Gregory Roby a écrit :
> Guillaume,
> 
> Thanks for the quick response, this nudges me in a direction that will
solve
> this (plus I can expand on it to solve a couple of other related issues I
am
> sure I will have in the coming month or so).
> 
> It sounds like the simplest way based on what I already have knowledge of
> then would be to:
> 
> Create a HTML form in which a user enters the point.
> Create a CGI Python program that gets the proper zoom-to extent (maybe
using
> GeoDjango, I have used Django a bit before).
> Dynamically output the new map webpage from the CGI program with the added
> Zoom to Extent Line.
> 
> On rereading it, it sounds a bit different from what you suggested but the
> same basic idea...  
>  
> I will post back in a week or so the results (and hopefully some code) in
> case anyone is interested.  
> 
> 
> Greg
> 
> 
> 
> 
> 
> -----Original Message-----
> From: Guillaume Sueur [mailto:guillaume.sueur at neogeo-online.net] 
> Sent: Thursday, April 10, 2008 3:48 PM
> To: groby
> Cc: users at openlayers.org
> Subject: Re: [OpenLayers-Users] Query Feature by Attribute and Zoom
> 
> Hi Greg,
> 
> As an WMS Server doesn't publish the features themselves, you will have 
> to use another method for the requested functionnality, even if the 
> layers can remain in WMS.
> 1. If you don't have so many countries, you can grab the extents of each 
> of them manually and build a simple html select list with the extents in 
> the value field. OnChange, you will fire a function getting the extent 
> and setting your map extent with it.
> 2. If you have many countries, or if these data change often, you can 
> build a simple web service sending you a stream (JSON for example) that 
> you will use to build dynamically the same list as above. On the server 
> side you can use many OpenSource tools to perform that : either 
> php/mapscript, or even ogrinfo, in fact any tool that will be able to 
> open your dataset and read the shapes in it. Of course, it's even 
> simpler if they are stored into postGIS ;-)
> 
> Hope that helps,
> 
> Guillaume
> 
> groby a écrit :
>> Hello,
>>
>> I am fairly new to OpenLayers and GIS in general and new to this list.  I
>> have just setup an OpenLayers page.  I am using MapServer as my WMS
Server
>> and my data is held in PostGIS tables. 
>>
>> I would like to know how can I create a way for a user to input text and
> to
>> zoom to the feature specified by the text.  
>>
>> An example of what I am trying to accomplish:  
>> Take the Feature Info Example at
>> http://www.openlayers.org/dev/examples/getfeatureinfo.html.  If I wanted
> to
>> make it so a user could type in a country name then have the map zoom to
> the
>> extent of the country, how would I do so?  It needs to be a general
enough
>> example I could use it with any text field (for example, a Serial
Number).
> 
>> I have found a few previous postings that seem to be asking a similar
>> question but not many answers.  Any pointers to a nice or ugly solution
> are
>> appreciated.
>>
>> Thanks.
>>
>> Greg
>>
>>
> 
> -
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> The information contained in this electronic mail transmission is intended
by Tri-County 
> Electric Cooperative for the use of the named individual or entity to
which it is directed 
> and may contain information that is confidential or privileged.  If you
have received this 
> electronic mail transmission in error, please delete it from your system
without copying or 
> forwarding it, and notify the sender of the error by reply email so that
the sender's address 
> records can be corrected.

-- 
Guillaume SUEUR
Expert SIG et OpenSource
NEOGEO
46 RUE MATABIAU
31000 TOULOUSE
06 74 91 95 20
Site web : http://www.neogeo-online.net


-------------- next part --------------
The information contained in this electronic mail transmission is intended by Tri-County 
Electric Cooperative for the use of the named individual or entity to which it is directed 
and may contain information that is confidential or privileged.  If you have received this 
electronic mail transmission in error, please delete it from your system without copying or 
forwarding it, and notify the sender of the error by reply email so that the sender's address 
records can be corrected.


More information about the Users mailing list