Tuesday, September 13, 2016

CORS with Geoserver 2.9.0 and Tomcat


How to enable CORS on your Geoserver 2.9.0 running on Tomcat.


This is taken directly from the tomcat homepage https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter


add the following code to your  web.xml  file then stop/start the tomcat process

service tomcat stop
service tomcat start

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.methods</param-name>
    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
  </init-param>
  <init-param>
    <param-name>cors.exposed.headers</param-name>
    <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
  </init-param>
  <init-param>
    <param-name>cors.support.credentials</param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>cors.preflight.maxage</param-name>
    <param-value>10</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Thursday, September 1, 2016

Using Django to create a point GeoJSON feature from the centroid of a polygon

A practical example showing how to use geoDjango, Django geospatial functions to create the centroid of a polygon, returning a GeoJSON dataset that includes all the attributes of the original polygon feature excluding the original  geometry.

We are using the Django Rest Framework to server up the response but you could of course use simple the Django response as well.

We pass in a unique_id value to get a single polygon feature to generate the centroid.

NOTE you will need to hook up your URLs to look something like this:

url(r'^getcenter/(?P<unique_id>.+/$', get_room_center, name='centroid'),