<?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>

				<!-- Oblique_Line_Latitude,
                                  Oblique_Line_Longitude, -->

				<xsl:if test="/MIF/Grid_Coordinate_System_Name or /MIF/Map_Projection_Name">
					<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,       
										 Straight_Verticle_Longitude_from_Pole,                                   
										 Standard_Parallel,       
										 Scale_Factor_at_Equator,       
										 Scale_Factor_at_Central_Meridian,                                   
										 Longitude_of_Central_Meridian,       
										 Height_of_Perspective_Point_Above_Surface,                                   
										 Latitude_of_Projection_Origin,       
										 Longitude_of_Projection_Center,       
										 Latitude_of_Projection_Center,       
										 Scale_Factor_at_Center_Line,       
										 Azimuthal_Angle,                                   
										 Azimuth_Measure_Point_Longitude,       
										 Scale_Factor_at_Projection_Origin,       
										 Landsat_Number,      
									         Path_Number,                                   
									         False_Easting,                                   
									         False_Northing,                                                                                                                                                                                                                                                                                                                                                                                                     
									         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:if test="/MIF/Grid_Coordinate_System_Name = 'Other Grid Systems Definition'">
									 <!-- <xsl:apply-templates select="."/> -->
									  <xsl:apply-templates select="/MIF/Other_Grid_Systems_Definition"/>
									 </xsl:if>


									 <!-- name()='Other_Grid_Systems_Definition' or -->
									<!--<xsl:if test="/MIF/Grid_Coordinate_System_Name != 'Other Grid Systems Definition' or
		/MIF/Grid_Coordinate_System_Name !='Oblique Mercator'"> -->
									 <xsl:if test="/MIF/Grid_Coordinate_System_Name != 'Other Grid Systems Definition'">
									  <xsl:element name="{translate(/MIF/Map_Projection_Name[@gid1=$gid1], ' ', '_')}">
									   <xsl:if test="/MIF/Map_Projection_Name !='Oblique Mercator'">

									    <xsl:for-each select="current-group()">
									     <xsl:if test="name()='Scale_Factor_at_Central_Meridian' or                    
										           name()='Straight_Verticle_Longitude_from_Pole' or        
											   name()='Standard_Parallel' or      
											   name()='Scale_Factor_at_Central_Meridian' or      
											   name()='Height_of_Perspective_Point_Above_Surface' or      
											   name()='Scale_Factor_at_Equator' or                                         
											   name()='Longitude_of_Central_Meridian' or                                         
											   name()='Latitude_of_Projection_Origin' or                  
											   name()='Azimuthal_Angle' or                                         
											   name()='Azimuth_Measure_Point_Longitude' or      
											   name()='Scale_Factor_at_Projection_Origin' or      
											   name()='Landsat_Number' or      
											   name()='Path_Number' or                                         
											   name()='False_Easting' or                                         
											   name()='False_Northing' or                                                                                                                               
											   name()='Latitude_of_Projection_Center'or                                                                                                                           
											   name()='Oblique_Line_Latitude' or                                         
											   name()='Oblique_Line_Longitude' or                                                                                                                                                                    
											   name()='Pah_Number'                                         ">
								              <xsl:copy>
									       <xsl:apply-templates select="."/>
									      </xsl:copy>
									     </xsl:if>
									    </xsl:for-each>
									   </xsl:if>

													<xsl:if test="/MIF/Scale_Factor_at_Center_Line">


														<xsl:element name="Scale_Factor_at_Center_Line">
															<xsl:apply-templates select="/MIF/Scale_Factor_at_Center_Line"/>
														</xsl:element>

														<xsl:if test="/MIF/Azimuthal_Angle">
															<xsl:element name="Oblique_Line_Azimuth">

																<xsl:element name="Azimuthal_Angle">
																	<xsl:apply-templates select="/MIF/Azimuthal_Angle"/>
																</xsl:element>

																<xsl:element name="Azimuth_Measure_Point_Longitude">
																	<xsl:apply-templates select="/MIF/Azimuth_Measure_Point_Longitude"/>
																</xsl:element>
															</xsl:element>
														</xsl:if>

														<xsl:if test="/MIF/Oblique_Line_Latitude">
															<xsl:element name="Oblique_Line_Point">

																<xsl:if test="/MIF/Oblique_Line_Latitude/@gid1= '1'">
																	<xsl:element name="Oblique_Line_Latitude">
																		<xsl:value-of select="/MIF/Oblique_Line_Latitude[@gid1='1']"/>
																	</xsl:element>
																</xsl:if>

																<xsl:if test="/MIF/Oblique_Line_Longitude/@gid1= '1'">
																	<xsl:element name="Oblique_Line_Longitude">
																		<xsl:value-of select="/MIF/Oblique_Line_Longitude[@gid1='1']"/>
																	</xsl:element>
																</xsl:if>

																<xsl:if test="/MIF/Oblique_Line_Latitude/@gid1= '2'">
																	<xsl:element name="Oblique_Line_Latitude">
																		<xsl:value-of select="/MIF/Oblique_Line_Latitude[@gid1='2']"/>
																	</xsl:element>
																</xsl:if>

																<xsl:if test="/MIF/Oblique_Line_Longitude/@gid1= '2'">
																	<xsl:element name="Oblique_Line_Longitude">
																		<xsl:value-of select="/MIF/Oblique_Line_Longitude[@gid1='2']"/>
																	</xsl:element>
																</xsl:if>
															</xsl:element>
														</xsl:if>

														<xsl:element name="Latitude_of_Projection_Origin">
															<xsl:apply-templates select="/MIF/Latitude_of_Projection_Origin"/>
														</xsl:element>

														<xsl:element name="False_Easting">
															<xsl:apply-templates select="/MIF/False_Easting"/>
														</xsl:element>

														<xsl:element name="False_Northing">
															<xsl:apply-templates select="/MIF/False_Northing"/>
														</xsl:element>
													</xsl:if>
												</xsl:element>
											</xsl:if>
										</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>
										<!-- Height_of_Perspective_Point_Above_Surface -->
										<xsl:element name="{translate(/MIF/Map_Projection_Name[@gid1=$gid1], ' ', '_')}">
											<xsl:if test="/MIF/Map_Projection_Name !='Oblique Mercator'">

												<xsl:for-each select="current-group()">
													<xsl:if test="name()='Scale_Factor_at_Central_Meridian' or                    
														      name()='Straight_Verticle_Longitude_from_Pole' or        
														      name()='Standard_Parallel' or      
														      name()='Scale_Factor_at_Central_Meridian' or      
														      name()='Height_of_Perspective_Point_Above_Surface' or      
														      name()='Scale_Factor_at_Equator' or                                         
														      name()='Longitude_of_Central_Meridian' or                                         
														      name()='Latitude_of_Projection_Origin' or                  
														      name()='Azimuthal_Angle' or                                         
														      name()='Azimuth_Measure_Point_Longitude' or      
														      name()='Scale_Factor_at_Projection_Origin' or      
														      name()='Landsat_Number' or      
														      name()='Path_Number' or                                         
														      name()='False_Easting' or                                         
														      name()='False_Northing' or
														      name()='Longitude_of_Projection_Center'or                                                                                                                               
														      name()='Latitude_of_Projection_Center'or
									                                              name()='Oblique_Line_Latitude' or                                         
														      name()='Oblique_Line_Longitude' or                                                                                                                                                                    
														      name()='Pah_Number'                                         ">
														<xsl:copy>
															<xsl:apply-templates select="."/>
														</xsl:copy>
													</xsl:if>
												</xsl:for-each>
											</xsl:if>

											<xsl:if test="/MIF/Scale_Factor_at_Center_Line">


												<xsl:element name="Scale_Factor_at_Center_Line">
													<xsl:apply-templates select="/MIF/Scale_Factor_at_Center_Line"/>
												</xsl:element>

												<xsl:if test="/MIF/Azimuthal_Angle">
													<xsl:element name="Oblique_Line_Azimuth">

														<xsl:element name="Azimuthal_Angle">
															<xsl:apply-templates select="/MIF/Azimuthal_Angle"/>
														</xsl:element>

														<xsl:element name="Azimuth_Measure_Point_Longitude">
															<xsl:apply-templates select="/MIF/Azimuth_Measure_Point_Longitude"/>
														</xsl:element>
													</xsl:element>
												</xsl:if>

												<xsl:if test="/MIF/Oblique_Line_Latitude">
													<xsl:element name="Oblique_Line_Point">

														<xsl:if test="/MIF/Oblique_Line_Latitude/@gid1= '1'">
															<xsl:element name="Oblique_Line_Latitude">
																<xsl:value-of select="/MIF/Oblique_Line_Latitude[@gid1='1']"/>
															</xsl:element>
														</xsl:if>

														<xsl:if test="/MIF/Oblique_Line_Longitude/@gid1= '1'">
															<xsl:element name="Oblique_Line_Longitude">
																<xsl:value-of select="/MIF/Oblique_Line_Longitude[@gid1='1']"/>
															</xsl:element>
														</xsl:if>

														<xsl:if test="/MIF/Oblique_Line_Latitude/@gid1= '2'">
															<xsl:element name="Oblique_Line_Latitude">
																<xsl:value-of select="/MIF/Oblique_Line_Latitude[@gid1='2']"/>
															</xsl:element>
														</xsl:if>

														<xsl:if test="/MIF/Oblique_Line_Longitude/@gid1= '2'">
															<xsl:element name="Oblique_Line_Longitude">
																<xsl:value-of select="/MIF/Oblique_Line_Longitude[@gid1='2']"/>
															</xsl:element>
														</xsl:if>
													</xsl:element>
												</xsl:if>

												<xsl:element name="Latitude_of_Projection_Origin">
													<xsl:apply-templates select="/MIF/Latitude_of_Projection_Origin"/>
												</xsl:element>

												<xsl:element name="False_Easting">
													<xsl:apply-templates select="/MIF/False_Easting"/>
												</xsl:element>

												<xsl:element name="False_Northing">
													<xsl:apply-templates select="/MIF/False_Northing"/>
												</xsl:element>
											</xsl:if>
										</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>
				<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><template name="Spatial_Reference_Information"></template></MapperBlockPosition></MapperMetaTag>
</metaInformation>
-->
