-
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
Closed
Changes from 5 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
35 changes: 35 additions & 0 deletions
35
...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,35 @@ | ||
| /* | ||
| * 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) { | ||
| SpatialReferenceSystemInformation srsInfo = srsCache.getSrsInfo(srid); | ||
| return srsInfo != null ? srsInfo.stringId() : null; | ||
| } | ||
|
|
||
| // Returns the SRID corresponding to the input string ID. If not supported, returns `null`. | ||
| public static Integer getSrid(String stringId) { | ||
| SpatialReferenceSystemInformation srsInfo = srsCache.getSrsInfo(stringId); | ||
| return srsInfo != null ? srsInfo.srid() : null; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...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,35 @@ | ||
| /* | ||
| * 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) { | ||
| SpatialReferenceSystemInformation srsInfo = srsCache.getSrsInfo(srid); | ||
| return srsInfo != null && srsInfo.isGeographic() ? srsInfo.stringId() : null; | ||
| } | ||
|
|
||
| // Returns the SRID corresponding to the input string ID. If not supported, returns `null`. | ||
| public static Integer getSrid(String stringId) { | ||
| SpatialReferenceSystemInformation srsInfo = srsCache.getSrsInfo(stringId); | ||
| return srsInfo != null && srsInfo.isGeographic() ? srsInfo.srid() : null; | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
| /* | ||
| * 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 java.util.HashMap; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Class for maintaining the mappings between supported SRID/CRS values and the corresponding SRS. | ||
| */ | ||
| public class SpatialReferenceSystemCache { | ||
|
|
||
| // Private constructor to prevent external instantiation of this singleton class. | ||
| private SpatialReferenceSystemCache() { | ||
| populateSpatialReferenceSystemInformationMapping(); | ||
| } | ||
|
|
||
| // 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 SpatialReferenceSystemCache instance = null; | ||
|
|
||
| // 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. | ||
| public static SpatialReferenceSystemCache getInstance() { | ||
| if (instance == null) { | ||
| synchronized (SpatialReferenceSystemCache.class) { | ||
| if (instance == null) { | ||
| instance = new SpatialReferenceSystemCache(); | ||
| } | ||
| } | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| // 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); | ||
| } | ||
| } | ||
|
|
||
| // 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 { | ||
|
|
||
| // 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
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
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
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.