Australian Antarctic Division Conditions of Use | Feedback & requests | About us | Site map
Forgot Your Password?
Username:  
Password:
Australian Antarctic Data Centre

Australian Antarctic - C Squares

<!---
CF module: get_csquare
     Written by: Tony Rees, CSIRO Australia (Tony.Rees@csiro.au)
     Date Created: January, 2002
     Description: gets c-squares code for a supplied lat/long coordinate pair
     Inputs: latitude: latitude in decimal degrees (number)
             longitude: longitude in decimal degrees (number)
             resolution: square size in decimal degrees (number)
     Output: csqaures code as string
     Remarks: resolution currently expected as one of the following: 10, 5, 1, 0.5, 0.1 [degrees]
             0 deg latitude/longitude is treated as positive
             90 deg latitude is treated as 89.99999, -90 deg latitude is treated as -89.99999
             180 deg longitude is treated as 179.99999, -180 deg longitude is treated as -179.99999

--->
<cfparam name="attributes.resolution" default="1" type="numeric">

<cfset lat = attributes.latitude>
<cfset lon = attributes.longitude>

<!--- global quadrant --->
<cfif lat ge 0>
    <cfif lon ge 0.0> <cfset c1 = '1'> <cfelse> <cfset c1 = '7'> </cfif>
<cfelse>
    <cfif lon ge 0.0> <cfset c1 = '3'> <cfelse> <cfset c1 = '5'> </cfif>
</cfif>


<!--- get the next digit (tens of degrees latitude) --->
<cfif lat gt -90.0 and lat lt 90.0>
    <cfset c2 = int(abs(lat/10.0))>
    <cfset lat_remainder = abs(lat) - 10.0 * c2>
<cfelse>
    <cfset c2 = '8'>
    <cfset lat_remainder = 9.99999>
</cfif>

<!---  get the next 2 digits (tens of degrees longitude) --->
<cfif lat gt -90.0 and lat lt 90.0>
    <cfset c34 = NumberFormat(int(abs(lon/10.0)),'99')>
    <cfif mid(c34,1,1) is ' '> <cfset c34 = '0' & mid(c34,2,1)> </cfif>
    <cfset long_remainder = abs(lon) - 10.0 * c34>
<cfelse>
    <cfset c34 = '17'>
    <cfset long_remainder = 9.99999>
</cfif>


<cfif attributes.resolution lt 10.0>

       <!---get the 6th digit (single degrees latitude) --->
      <cfset c6 = int(lat_remainder)>
      <cfset lat_remainder = lat_remainder - c6>

       <!---get the 7th digit (single degrees longitude) --->
      <cfset c7 = int(long_remainder)>
      <cfset long_remainder = long_remainder - c7>

<!--- <cfoutput>C6 #c6# C7 #c7#</cfoutput><br> --->

       <!---get the 5th digit (5-degree quadrant) --->
      <cfset c5 = ' '>
      <cfif (c6 ge 0 and c6 le 4) and (c7 ge 0 and c7 le 4)>
           <cfset c5 = '1'>
      <cfelseif (c6 ge 0 and c6 le 4) and (c7 ge 5 and c7 le 9)>
           <cfset c5 = '2'>
      <cfelseif (c6 ge 5 and c6 le 9) and (c7 ge 0 and c7 le 4)>
           <cfset c5 = '3'>
      <cfelseif (c6 ge 5 and c6 le 9) and (c7 ge 5 and c7 le 9)>
           <cfset c5 = '4'>
      </cfif>

      <cfif attributes.resolution lt 1.0>

        <!---get the 9th digit (tenths of degrees latitude) --->
        <cfset c9 = int(lat_remainder*10)>
        <cfset lat_remainder = lat_remainder - c9/10>

         <!---- get the 10th digit (tenths of degrees longitude) --->
        <cfset c10  = int(long_remainder*10)>
        <cfset long_remainder = long_remainder - c10/10>

         <!---get the 8th digit (0.5-degree quadrant) --->
        <cfif (c9 ge 0 and c9 le 4) and (c10 ge 0 and c10 le 4)>
            <cfset c8 = '1'>
        <cfelseif (c9 ge 0 and c9 le 4) and (c10 ge 5 and c10 le 9)>
            <cfset c8 = '2'>
        <cfelseif (c9 ge 5 and c9 le 9) and (c10 ge 0 and c10 le 4)>
            <cfset c8 =  '3'>
        <cfelseif (c9 ge 5 and c9 le 9) and (c10 ge 5 and c10 le 9)>
            <cfset c8 =  '4'>
        </cfif>

      </cfif>

</cfif>


<cfif abs(attributes.resolution -10.0) lt 0.001>              <!--- 10 deg. resolution, e.g. Hobart: "3414" --->
      <cfset caller.csquare = c1 & c2 & c34>

<cfelseif abs(attributes.resolution - 5.0) lt 0.001>           <!--- 5 deg. resolution, e.g. Hobart: "3414:2" --->
      <cfset caller.csquare = c1 & c2 & c34 & ':' & c5>

<cfelseif abs(attributes.resolution - 1.0)  lt 0.001>           <!--- 1 deg. resolution, e.g. Hobart: "3414:227" --->
      <cfset caller.csquare = c1 & c2 & c34 & ':' & c5 & c6 & c7>

<cfelseif abs(attributes.resolution - 0.5) lt 0.001>           <!--- 0.5 deg. resolution, e.g. Hobart: "3414:227:3" --->
      <cfset caller.csquare = c1 & c2 & c34 & ':' & c5 & c6 & c7 & ':' & c8>

<cfelseif abs(attributes.resolution - 0.1) lt 0.001>           <!--- 0.1 deg. resolution, e.g. Hobart: "3414:227:383" --->
      <cfset caller.csquare = c1 & c2 & c34 & ':' & c5 & c6 & c7 & ':' & c8 & c9 & c10>
</cfif>

<cfif IsDefined("attributes.get_bounds")>
   <cfmodule template="get_csquare_bounds.cfm" csquare="#caller.csquare#">
   <cfset caller.north = north>
   <cfset caller.south = south>
   <cfset caller.east = east>
   <cfset caller.west = west>
</cfif>

Mapping Home Page | Antarctic Names | Antarctic Map Catalogue