<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
   Name:  SpatialReferenceInformation.xsl
   Author:  James W. Allor
   Date:  20050906
   Description:  XSLT stylesheet that transforms the Spatial Reference
                 Information from the MIF to the MRF.
                 Please reference the MIF Specification and the GPMS Table 1.
   Modification History:
      Initial   Date      Change Request ID   Description
      JWA       20050906   N/A                 Initial Version 
      JWA       20051109   N/A                 Updated to reflect current MIF Spec..
      MMc	20061115   N/A                 Fixed errors with the Grid Coordinate System 	
-->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>


<xsl:template name="Spatial_Reference_Information">
 
  <xsl:element name="Spatial_Reference_Information">

    <xsl:element name="Horizontal_Coordinate_System_Definition">

      <xsl:if test="/MIF/Latitude_Resolution">
        <xsl:element name="Geographic">
          <xsl:copy-of select="Latitude_Resolution"/>
          <xsl:copy-of select="Longitude_Resolution"/>
          <xsl:copy-of select="Geographic_Coordinate_Units"/>
        </xsl:element>
      </xsl:if>

          
      <xsl:for-each-group select="Grid_Coordinate_System_Name,
                                  UTM_Zone_Number,
                                  UPS_Zone_Identifier,
                                  SPCS_Zone_Identifier,
                                  ARC_System_Zone_Identifier,
                                  Other_Grid_Systems_Definition,
                                  Map_Projection_Name,
                                  Standard_Parallel,
				  Scale_Factor_at_Central_Meridian,
                                  Longitude_of_Central_Meridian,
                                  Latitude_of_Projection_Origin,
                                  False_Easting,
                                  False_Northing,
                                  Scale_Factor_at_Equator,
                                  Height_of_Perspective_Point_Above_Surface,
                                  Longitude_of_Projection_Center,
                                  Latitude_of_Projection_Center,
                                  Scale_Factor_At_Center_Line,
                                  Azimuthal_Angle,
                                  Azimuth_Measure_Point_Longitude,
                                  Oblique_Line_Latitude,
                                  Oblique_Line_Longitude,
                                  Straight_Vertical_Longitude_from_Pole,
                                  Scale_Factor_at_Projection_Origin,
                                  Landsat_Number,
                                  Path_Number,                                 
                                  Other_Projections_Definition,
                                  Planar_Coordinate_Encoding_Method,
                                  Abscissa_Resolution,
                                  Ordinate_Resolution,
                                  Planar_Distance_Units"
                          group-by="@gid1">
        <xsl:sort select="current-grouping-key()" data-type="number"/>
        <xsl:sort select="current-grouping-key()" data-type="text"/>

        <xsl:variable name="gid1" select="current-grouping-key()"/>

        <xsl:element name="Planar">
<!-- Scale_Factor_at_Central_Meridian, -->
          <xsl:choose>
  
            <xsl:when test="/MIF/Grid_Coordinate_System_Name[@gid1=$gid1]">

              <xsl:element name="Grid_Coordinate_System">
                
		<!-- Print out the element Grid_Coordinate_System_Name and prints out the value -->
		<xsl:copy>
                  <xsl:apply-templates select="/MIF/Grid_Coordinate_System_Name[@gid1=$gid1]"/>
                </xsl:copy>

  <xsl:element name="{translate(/MIF/Grid_Coordinate_System_Name[@gid1=$gid1], ' ', '_')}">               
                 <xsl:for-each select="current-group()">
                    
		    <xsl:if test="name()='UTM_Zone_Number' or
		                  name()='Map_Projection_Name' or
                                  name()='UPS_Zone_Identifier' or
                                  name()='SPCS_Zone_Identifier' or
                                  name()='ARC_System_Zone_Identifier'">
                      <xsl:copy>
                        <xsl:apply-templates select="."/>
                      </xsl:copy>
                    </xsl:if> 
 </xsl:for-each>
 
                   

                      <xsl:element name="{translate(/MIF/Map_Projection_Name[@gid1=$gid1], ' ', '_')}">
                        <xsl:for-each select="current-group()">
			
                          <xsl:if test="name()='Scale_Factor_at_Central_Meridian' or
			  		name()='Standard_Parallel' or
					name()='Scale_Factor_at_Central_Meridian' or
                                        name()='Longitude_of_Central_Meridian' or
                                        name()='Latitude_of_Projection_Origin' or
                                        name()='False_Easting' or
                                        name()='False_Northing' or
                                        name()='Scale_Factor_at_Equator' or
                                        name()='Height_of_Perspective_Point_Above_Surface' or
                                        name()='Longitude_of_Projection_Center' or
                                        name()='Latitude_of_Projection_Center' or
                                        name()='Scale_Factor_At_Center_Line' or
                                        name()='Azimuthal_Angle' or
                                        name()='Azimuth_Measure_Point_Longitude' or
                                        name()='Oblique_Line_Latitude' or
                                        name()='Oblique_Line_Longitude' or
                                        name()='Straight_Vertical_Longitude_from_Pole' or
                                        name()='Scale_Factor_at_Projection_Origin' or
                                        name()='Landsat_Number' or
                                        name()='Path_Number'
                                        ">
                            <xsl:copy>
                              <xsl:apply-templates select="."/>
                            </xsl:copy>
                          </xsl:if>
                        </xsl:for-each>
                      </xsl:element>
		     
</xsl:element>
</xsl:element>
                                 

            </xsl:when>


            <xsl:when test="/MIF/Map_Projection_Name[@gid1=$gid1] and
                            not(/MIF/Grid_Coordinate_System_Name[@gid1=$gid1])">

              <xsl:element name="Map_Projection">
                <xsl:copy>
                  <xsl:apply-templates select="/MIF/Map_Projection_Name[@gid1=$gid1]"/>
                </xsl:copy>

                <xsl:element name="{translate(/MIF/Map_Projection_Name[@gid1=$gid1], ' ', '_')}">
                  <xsl:for-each select="current-group()">
		  
                    <xsl:if test="name()='Scale_Factor_at_Central_Meridian' or
		                  name()='Standard_Parallel' or
                                  name()='Longitude_of_Central_Meridian' or
                                  name()='Latitude_of_Projection_Origin' or
                                  name()='False_Easting' or
                                  name()='False_Northing' or
                                  name()='Scale_Factor_at_Equator' or
                                  name()='Height_of_Perspective_Point_Above_Surface' or
                                  name()='Longitude_of_Projection_Center' or
                                  name()='Latitude_of_Projection_Center' or
                                  name()='Scale_Factor_At_Center_Line' or
                                  name()='Azimuthal_Angle' or
                                  name()='Azimuth_Measure_Point_Longitude' or
                                  name()='Oblique_Line_Latitude' or
                                  name()='Oblique_Line_Longitude' or
                                  name()='Straight_Vertical_Longitude_from_Pole' or
                                  name()='Scale_Factor_at_Projection_Origin' or
                                  name()='Landsat_Number' or
                                  name()='Path_Number' ">
                      <xsl:copy>
                        <xsl:apply-templates select="."/>
                      </xsl:copy>
                    </xsl:if>
                  </xsl:for-each>
                </xsl:element>

              </xsl:element>

            </xsl:when>

          </xsl:choose>

          <xsl:for-each select="current-group()">
            <xsl:if test="name()='Planar_Coordinate_Encoding_Method' or
                          name()='Abscissa_Resolution' or
                          name()='Ordinate_Resolution' or
                          name()='Planar_Distance_Units'">                                  
              <xsl:copy>
                <xsl:apply-templates select="."/>
              </xsl:copy>
            </xsl:if>
          </xsl:for-each>


        </xsl:element>

      </xsl:for-each-group>


      <xsl:if test="/MIF/Local_Description">
        <xsl:element name="Local">
          <xsl:for-each select="Local_Description,
                                Local_Georeference_Information">
            <xsl:copy>
              <xsl:apply-templates select="."/>
            </xsl:copy>
          </xsl:for-each>
        </xsl:element>
      </xsl:if>


      <xsl:element name="Geodetic_Model">
        <xsl:copy-of select="Horizontal_Datum_Name"/>
        <xsl:copy-of select="Ellipsoid_Name"/>
        <xsl:copy-of select="Semi-Major_Axis"/>
        <xsl:copy-of select="Denominator_of_Flattening_Ratio"/>
      </xsl:element>

    </xsl:element>

  </xsl:element>

</xsl:template>


</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c) 2004-2005. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios/><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition></MapperMetaTag>
</metaInformation>
-->
