james-willis commented on code in PR #1780: URL: https://github.com/apache/sedona/pull/1780#discussion_r1937804017
########## spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/adapters/StructuredAdapter.scala: ########## @@ -0,0 +1,206 @@ +/* + * 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.sedona_sql.adapters + +import org.apache.sedona.core.spatialRDD.SpatialRDD +import org.apache.sedona.sql.utils.GeometrySerializer +import org.apache.spark.api.java.JavaPairRDD +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.types.StructType +import org.apache.spark.sql.{DataFrame, Row, SparkSession} +import org.locationtech.jts.geom.Geometry +import org.slf4j.{Logger, LoggerFactory} + +object StructuredAdapter { + val logger: Logger = LoggerFactory.getLogger(getClass) + + /** + * Convert RDD[Row] to SpatialRDD. It puts Row as user data of Geometry. + * @param rdd + * @param geometryFieldName + * @return + */ + def toSpatialRdd(rdd: RDD[Row], geometryFieldName: String): SpatialRDD[Geometry] = { + val spatialRDD = new SpatialRDD[Geometry] + spatialRDD.schema = rdd.first().schema + spatialRDD.rawSpatialRDD = rdd + .map(row => { + val geom = row.getAs[Geometry](geometryFieldName) Review Comment: Should we implement the logic to skip over the geom when encoding user data? ########## spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/adapters/StructuredAdapter.scala: ########## @@ -0,0 +1,206 @@ +/* + * 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.sedona_sql.adapters + +import org.apache.sedona.core.spatialRDD.SpatialRDD +import org.apache.sedona.sql.utils.GeometrySerializer +import org.apache.spark.api.java.JavaPairRDD +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.types.StructType +import org.apache.spark.sql.{DataFrame, Row, SparkSession} +import org.locationtech.jts.geom.Geometry +import org.slf4j.{Logger, LoggerFactory} + +object StructuredAdapter { + val logger: Logger = LoggerFactory.getLogger(getClass) + + /** + * Convert RDD[Row] to SpatialRDD. It puts Row as user data of Geometry. + * @param rdd + * @param geometryFieldName + * @return + */ + def toSpatialRdd(rdd: RDD[Row], geometryFieldName: String): SpatialRDD[Geometry] = { + val spatialRDD = new SpatialRDD[Geometry] + spatialRDD.schema = rdd.first().schema + spatialRDD.rawSpatialRDD = rdd + .map(row => { + val geom = row.getAs[Geometry](geometryFieldName) + geom.setUserData(row.copy()) + geom + }) + .toJavaRDD() + spatialRDD + } + + /** + * Convert RDD[Row] to SpatialRDD. It puts Row as user data of Geometry. It auto-detects + * geometry column if geometryFieldName is not provided. It uses the first geometry column in + * RDD. + * @param rdd + * @return + */ + def toSpatialRdd(rdd: RDD[Row]): SpatialRDD[Geometry] = { + toSpatialRdd(rdd, firstGeomColName(rdd.first().schema)) + } + + /** + * Convert SpatialRDD to RDD[Row]. It extracts Row from user data of Geometry. + * @param spatialRDD + * @return + */ + def toRowRdd(spatialRDD: SpatialRDD[Geometry]): RDD[Row] = { + spatialRDD.rawSpatialRDD.map(geometry => { + val row = geometry.getUserData.asInstanceOf[Row] + row + }) + } + + /** + * Convert DataFrame to SpatialRDD. It puts InternalRow as user data of Geometry. It allows only + * one geometry column. + * + * @param dataFrame + * @param geometryFieldName + */ + def toSpatialRdd(dataFrame: DataFrame, geometryFieldName: String): SpatialRDD[Geometry] = { + val spatialRDD = new SpatialRDD[Geometry] + spatialRDD.schema = dataFrame.schema + val ordinal = spatialRDD.schema.fieldIndex(geometryFieldName) + spatialRDD.rawSpatialRDD = dataFrame.queryExecution.toRdd + .map(row => { + val geom = GeometrySerializer.deserialize(row.getBinary(ordinal)) + geom.setUserData(row.copy()) + geom + }) + .toJavaRDD() + spatialRDD + } + + /** + * Convert DataFrame to SpatialRDD. It puts InternalRow as user data of Geometry. It + * auto-detects geometry column if geometryFieldName is not provided. It uses the first geometry + * column in DataFrame. + * @param dataFrame + * @return + */ + def toSpatialRdd(dataFrame: DataFrame): SpatialRDD[Geometry] = { + toSpatialRdd(dataFrame, firstGeomColName(dataFrame.schema)) Review Comment: use getGeometryColumn name instead of firstGeomColName: https://github.com/apache/sedona/blob/master/spark/common/src/main/scala/org/apache/sedona/util/DfUtils.scala#L25 ########## spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/adapters/StructuredAdapter.scala: ########## @@ -0,0 +1,206 @@ +/* + * 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.sedona_sql.adapters + +import org.apache.sedona.core.spatialRDD.SpatialRDD +import org.apache.sedona.sql.utils.GeometrySerializer +import org.apache.spark.api.java.JavaPairRDD +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.types.StructType +import org.apache.spark.sql.{DataFrame, Row, SparkSession} +import org.locationtech.jts.geom.Geometry +import org.slf4j.{Logger, LoggerFactory} + +object StructuredAdapter { + val logger: Logger = LoggerFactory.getLogger(getClass) + + /** + * Convert RDD[Row] to SpatialRDD. It puts Row as user data of Geometry. + * @param rdd + * @param geometryFieldName + * @return + */ + def toSpatialRdd(rdd: RDD[Row], geometryFieldName: String): SpatialRDD[Geometry] = { + val spatialRDD = new SpatialRDD[Geometry] + spatialRDD.schema = rdd.first().schema + spatialRDD.rawSpatialRDD = rdd + .map(row => { + val geom = row.getAs[Geometry](geometryFieldName) + geom.setUserData(row.copy()) + geom + }) + .toJavaRDD() Review Comment: is this necessary? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
