- 
                Notifications
    You must be signed in to change notification settings 
- Fork 28.9k
[SPARK-53957][Geo][SQL] Support GEOGRAPHY and GEOMETRY in the SpatialReferenceSystemMapper #52667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
      
        
          +271
        
        
          −116
        
        
          
        
      
    
  
  
     Closed
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8165284
              
                Initial commit
              
              
                uros-db f1b3941
              
                Merge branch 'apache:master' into geo-srsMapper
              
              
                uros-db 9b5adfe
              
                Refactor SRS mapping
              
              
                uros-db 2b17eb8
              
                Avoid double lookup
              
              
                uros-db 12451f0
              
                Remove SingletonBase
              
              
                uros-db 5f0f540
              
                Use Java record
              
              
                uros-db File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            33 changes: 33 additions & 0 deletions
          
          33 
        
  ...main/scala/org/apache/spark/sql/internal/types/CartesianSpatialReferenceSystemMapper.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|  | ||
| package org.apache.spark.sql.internal.types; | ||
|  | ||
| /** | ||
| * Class for providing SRS mappings for cartesian spatial reference systems. | ||
| */ | ||
| public class CartesianSpatialReferenceSystemMapper extends SpatialReferenceSystemMapper { | ||
| // Returns the string ID corresponding to the input SRID. If not supported, returns `null`. | ||
| public static String getStringId(int srid) { | ||
| return srsCache.isValidSrid(srid) ? srsCache.getSrsInfo(srid).stringId() : null; | ||
| } | ||
|  | ||
| // Returns the SRID corresponding to the input string ID. If not supported, returns `null`. | ||
| public static Integer getSrid(String stringId) { | ||
| return srsCache.isValidStringId(stringId) ? srsCache.getSrsInfo(stringId).srid() : null; | ||
|         
                  uros-db marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| } | ||
        
          
          
            33 changes: 33 additions & 0 deletions
          
          33 
        
  ...ain/scala/org/apache/spark/sql/internal/types/GeographicSpatialReferenceSystemMapper.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|  | ||
| package org.apache.spark.sql.internal.types; | ||
|  | ||
| /** | ||
| * Class for providing SRS mappings for geographic spatial reference systems. | ||
| */ | ||
| public class GeographicSpatialReferenceSystemMapper extends SpatialReferenceSystemMapper { | ||
| // Returns the string ID corresponding to the input SRID. If not supported, returns `null`. | ||
| public static String getStringId(int srid) { | ||
| return srsCache.isGeographicSrid(srid) ? srsCache.getSrsInfo(srid).stringId() : null; | ||
|         
                  uros-db marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
|  | ||
| // Returns the SRID corresponding to the input string ID. If not supported, returns `null`. | ||
| public static Integer getSrid(String stringId) { | ||
| return srsCache.isGeographicStringId(stringId) ? srsCache.getSrsInfo(stringId).srid() : null; | ||
|         
                  uros-db marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| } | ||
        
          
          
            98 changes: 98 additions & 0 deletions
          
          98 
        
  sql/api/src/main/scala/org/apache/spark/sql/internal/types/SpatialReferenceSystemCache.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|  | ||
| package org.apache.spark.sql.internal.types; | ||
|  | ||
| import org.apache.spark.sql.internal.util.SingletonBase; | ||
|  | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|  | ||
| /** | ||
| * Class for maintaining the mappings between supported SRID/CRS values and the corresponding SRS. | ||
| */ | ||
| public class SpatialReferenceSystemCache extends SingletonBase<SpatialReferenceSystemCache> { | ||
|  | ||
| // Private constructor to prevent external instantiation. | ||
| private SpatialReferenceSystemCache() { | ||
| populateSpatialReferenceSystemInformationMapping(); | ||
| } | ||
|  | ||
| @Override | ||
| protected SpatialReferenceSystemCache createInstance() { | ||
| return new SpatialReferenceSystemCache(); | ||
| } | ||
|  | ||
| public static SpatialReferenceSystemCache getInstance() { | ||
| return SingletonBase.getInstance(new SpatialReferenceSystemCache()); | ||
| } | ||
|  | ||
| // Hash map for defining the mappings from the integer SRID value to the full SRS information. | ||
| private final HashMap<Integer, SpatialReferenceSystemInformation> sridToSrs = | ||
| new HashMap<>(); | ||
|  | ||
| // Hash map for defining the mappings from the string ID value to the full SRS information. | ||
| private final HashMap<String, SpatialReferenceSystemInformation> stringIdToSrs = | ||
| new HashMap<>(); | ||
|  | ||
| // Helper method for building the SRID-to-SRS and stringID-to-SRS mappings. | ||
| private void populateSpatialReferenceSystemInformationMapping() { | ||
| // Currently, we only support a limited set of SRID / CRS values. However, we will soon extend | ||
| // this to support all the SRIDs supported by relevant authorities and libraries. The SRS list | ||
| // below will be updated accordingly, and the maps will be populated with more complete data. | ||
| List<SpatialReferenceSystemInformation> srsInformationList = List.of( | ||
| new SpatialReferenceSystemInformation(0, "SRID:0", false), | ||
| new SpatialReferenceSystemInformation(3857, "EPSG:3857", false), | ||
| new SpatialReferenceSystemInformation(4326, "OGC:CRS84", true) | ||
| ); | ||
| // Populate the mappings using the same SRS information objects, avoiding any duplication. | ||
| for (SpatialReferenceSystemInformation srsInformation: srsInformationList) { | ||
| sridToSrs.put(srsInformation.srid(), srsInformation); | ||
| stringIdToSrs.put(srsInformation.stringId(), srsInformation); | ||
| } | ||
| } | ||
|  | ||
| // Utility method that can be used to check whether an SRID value is valid or not. | ||
| public boolean isValidSrid(int srid) { | ||
| return sridToSrs.containsKey(srid); | ||
| } | ||
|  | ||
| // Utility method that can be used to check whether an SRID value is geographic or not. | ||
| public boolean isGeographicSrid(int srid) { | ||
| return isValidSrid(srid) && sridToSrs.get(srid).isGeographic(); | ||
| } | ||
|  | ||
| // Utility method that can be used to check whether a string ID value is valid or not. | ||
| public boolean isValidStringId(String stringId) { | ||
| return stringIdToSrs.containsKey(stringId); | ||
| } | ||
|  | ||
| // Utility method that can be used to check whether a string ID value is geographic or not. | ||
| public boolean isGeographicStringId(String stringId) { | ||
| return isValidStringId(stringId) && stringIdToSrs.get(stringId).isGeographic(); | ||
| } | ||
|  | ||
|         
                  uros-db marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| // Returns the SRS corresponding to the input SRID. If not supported, returns `null`. | ||
| public SpatialReferenceSystemInformation getSrsInfo(int srid) { | ||
| return sridToSrs.getOrDefault(srid, null); | ||
| } | ||
|  | ||
| // Returns the SRS corresponding to the input string ID. If not supported, returns `null`. | ||
| public SpatialReferenceSystemInformation getSrsInfo(String stringId) { | ||
| return stringIdToSrs.getOrDefault(stringId, null); | ||
| } | ||
| } | ||
        
          
          
            55 changes: 55 additions & 0 deletions
          
          55 
        
  ...src/main/scala/org/apache/spark/sql/internal/types/SpatialReferenceSystemInformation.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|  | ||
| package org.apache.spark.sql.internal.types; | ||
|  | ||
| /** | ||
| * Class for maintaining information about a spatial reference system (SRS). | ||
| */ | ||
| public class SpatialReferenceSystemInformation { | ||
|         
                  uros-db marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| // Field storing the spatial reference identifier (SRID) value of this SRS. | ||
| private final int srid; | ||
|  | ||
| // Field storing the string ID of the corresponding coordinate reference system (CRS). | ||
| private final String stringId; | ||
|  | ||
| // Field indicating whether the spatial reference system (SRS) is geographic or not. | ||
| private final boolean isGeographic; | ||
|  | ||
| // Constructor for creating an instance of the spatial reference system information class. | ||
| public SpatialReferenceSystemInformation(int srid, String stringId, boolean isGeographic) { | ||
| this.srid = srid; | ||
| this.stringId = stringId; | ||
| this.isGeographic = isGeographic; | ||
| } | ||
|  | ||
| // Specialized getters for each of the spatial reference system (SRS) information fields. | ||
|  | ||
| public int srid() { | ||
| return srid; | ||
| } | ||
|  | ||
| public String stringId() { | ||
| return stringId; | ||
| } | ||
|  | ||
| public boolean isGeographic() { | ||
| return isGeographic; | ||
| } | ||
|  | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            46 changes: 46 additions & 0 deletions
          
          46 
        
  sql/api/src/main/scala/org/apache/spark/sql/internal/util/SingletonBase.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|  | ||
| package org.apache.spark.sql.internal.util; | ||
|  | ||
| /** | ||
| * Generic abstract base class for implementing the singleton design pattern. This class is lazily | ||
| * initialized, thread-safe, and generic (it can be used to create singletons for any type `T`). | ||
|         
                  uros-db marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| */ | ||
| public abstract class SingletonBase<T> { | ||
| // The singleton `instance` is created lazily, meaning that it is not instantiated until the | ||
| // `getInstance()` method is called for the first time. Note that this solution is thread-safe. | ||
| private static volatile Object instance = null; | ||
|  | ||
| // The `createInstance` method is abstract, so all subclasses must implement it. Note that | ||
| // this method defines how the singleton instance is created for the specific subclass. | ||
| protected abstract T createInstance(); | ||
|  | ||
| // The `getInstance` method uses double-checked locking to ensure efficient and safe instance | ||
| // creation. The singleton instance is created only once, even in a multithreaded environment. | ||
| @SuppressWarnings("unchecked") | ||
| public static <T> T getInstance(SingletonBase<T> singletonBase) { | ||
| if (instance == null) { | ||
| synchronized (SingletonBase.class) { | ||
| if (instance == null) { | ||
| instance = singletonBase.createInstance(); | ||
| } | ||
| } | ||
| } | ||
| return (T) instance; | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.