From charlesreid1

Line 193: Line 193:
     jsonpCallback : 'getJson',
     jsonpCallback : 'getJson',
</source>
</source>
===Confused===
This is what's driving me nuts: a whole bunch of websites vaguely mention this piece of code:
<pre>
OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";
</pre>
But NO ONE SAYS WHERE IT IS SUPPOSED TO GO. The apache configuration file? But how does it know what OpenLayers is? It doesn't go in proxy.cgi - it isn't valid Python. Does it go in the Javascript performing the request? But that doesn't use OpenLayers.
Total lack of clarity from the documentation, compounded by everyone parroting what the documentation says without providing any further illumination on the issue at all.


===Still Not Working===
===Still Not Working===

Revision as of 04:42, 2 February 2015

This guide (part 2) picks up where the Geodroplet page (part 1) left off:

http://docs.geoserver.org/stable/en/user/gettingstarted/web-admin-quickstart/index.html

Logging In

Username/Password

Default username/pw are admin/geoserver. First thing I did was change those.

Server Status

Server status page shows some useful info:

GeoserverServerStatus.png

Data

On the left hand side menu, there is a cluster of items labeled "Data".

Here is what "Stores" looks like:

GeoserverStores.png

There's also "Layers," "Workspaces," etc. Not clear what all of this is for, I'm just browsing through it... Geoserver documentation isn't saying much about what these things are. (This is the web interface quick start, after all...)

Tutorial

Here is a tutorial: http://docs.geoserver.org/stable/en/user/gettingstarted/shapefile-quickstart/index.html

Importing Shape Data

Download the NYC roads shape file package linked to in the tutorial.

This goes in the Geoserver data directory. Remember from the Geodroplet page that this is a directory in our Tomcat webapp directory, so the whole thing is at /var/lib/tomcat7/webapps/geoserver/data/data (NOTE THE DOUBLE DATA!).

Move the map data to the Geoserver data directory:

sudo mv nyc_roads /var/lib/tomcat7/webapps/geoserver/data/data/.

You'll also have to change the owner to the Tomcat user:

sudo chown -R tomcat7:tomcat7 /var/lib/tomcat7/webapps/geoserver/data/data/nyc_roads/

Now we keep following the instructions.

Creating Workspace

Still logged into the Geoserver web portal as the admin, we go to Data and Workspaces, on the left hand side menu.

We pick add a new workspace, name it and populate it following the tutorial instructions [1].

Creating Store

Followed the tutorial instructions to create a store for NYC roads data...

Creating Layer

Created a layer for looking at that map... this is a lot of steps, making for a cumbersome workflow...

And finally, the document ends, at an anticlimactic moment. The map isn't that interesting, there are no other satellite or other map layers on it, and I'm not sure what to do with this.

[2]


Using Data from the Server

In order to test out the server's capabilities, I wanted to visualize the New York City streets dataset that I just imported in a site with an embedded map. For this purpose, I can use Leaflet, the Javascript maps app. I'll feed Leaflet a GeoJSON URL for the New York City street data, which Geoserver will serve up.

This will have three parts:

  • HTML (and CSS)
  • Javascript
  • URL for GeoJSON data

Before We Begin

The first thing we have to do, in order to access GeoJSON data via a URL, is fix the security settings of Geoserver. Right now, it is only set to serve up data to requests from "localhost." We will be accessing it from its IP address (and potentially other IP addresses, if it is a public server).

The error I was seeing was:

 XMLHttpRequest cannot load http://AAA.BBB.CCC.DDD:8080/geoserver/nyc_roads/ows?service=WFS&version=1.0.…&typeName=nyc_roads:nyc_roads&maxFeatures=50&outputFormat=application/json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://AAA.BBB.CCC.DDD' is therefore not allowed access.

Setting Up a Proxy Pass

Searching for this error yielded a stack overflow question that mentioned a Proxy Pass for Apache that needed to be set up [3]

The Geoserver tutorial states that to make requests from places other than "localhost", you have to set up an Apache proxy pass [4]

This involved editing the apache configuration file, at /etc/apache2/sites-available/000-default.conf on default Apache installations on Ubuntu systems, and added the following just before the VirtualHost *:80 tag:

ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /geoserver http://localhost:8080/geoserver
ProxyPassReverse /geoserver http://localhost:8080/geoserver

In plain English: when an outside server accesses the /geoserver directory, it is passed through a local proxy.

What a local proxy does is take requests from non-local servers to access the Geoserver, and makes them look like local requests.

Note that you'll have to do this, even if you're accessing Geoserver from the same IP address and the same machine, as long as you don't access Geoserver using an address like localhost:8080.

Enabling Apache Proxy Mod

I had to take one more step, before this worked, since my Apache installation didn't have the Proxy module enabled by default.

I ran this command to enable the Proxy mod:

sudo a2enmod proxy

after which, I restarted apache:

sudo service apache2 restart

Creating Proxy CGI File

Now we have to create a proxy CGI file:

/var/www/html/cgi-bin/proxy.cgi

The tutorial says to use this example one: http://trac.osgeo.org/openlayers/browser/trunk/openlayers/examples/proxy.cgi

But that's confusing. This person says to include a single line: http://gis.stackexchange.com/questions/83272/geoserver-access-control-allow-origin.

Python CGI Script

The tutorial links to a Python CGI script, which was actually for OpenLayers. I am using Leaflet.

But I enabled Apache CGI anyway.

sudo a2enmod cgi

This creates a directory for CGI scripts owned by root, at /usr/lib/cgi-bin.

Geoserver Use JSONP

Following this page [5] I enabled JSONP by editing /var/lib/tomcat7/webapps/geoserver/WEB-INF/web.xml as sudo, and adding this block:

  <context-param>
    <param-name>ENABLE_JSONP</param-name>
    <param-value>true</param-value>
  </context-param>

Now, when I make a request to my Geoserver in my Javascript code, it will specify the format as JSONP. My javascript code will look like this:

var owsrootUrl = 'http://AA.BB.CC.DD:8080/geoserver/ows';

var defaultParameters = {
    service : 'WFS',
    version : '1.0',
    request : 'GetFeature',
    typeName : 'nyc_roads:nyc_roads',
    maxFeatures : '100',
    outputFormat : 'text/javascript',
    format_options : 'callback:getJson',
};

var parameters = L.Util.extend(defaultParameters);
var URL = owsrootUrl + L.Util.getParamString(parameters);

and when we use some ajax for the map features, it will look like:

$.ajax({
    type: "POST",
    url: URL,
    dataType: 'jsonp',
    jsonpCallback : 'getJson',

Still Not Working

This is driving me nuts. It still isn't working. Probably because I am not using the above piece of code, which I can't do without knowing where to put it.

Creating the HTML Site