- 之前寫(xiě)過(guò)關(guān)于ST_PointFromText眷射,但是有一個(gè)點(diǎn)忽略了暂刘,那就重新寫(xiě)一遍
完整代碼:
# coding=utf-8
import findspark
from geospark.core.SpatialRDD import PointRDD
from geospark.core.enums import FileDataSplitter
from pyspark.sql.functions import expr, col
findspark.init()
from geospark.utils import KryoSerializer, GeoSparkKryoRegistrator
from pyspark.sql import SparkSession
from geospark.register import GeoSparkRegistrator
from geospark.utils.adapter import Adapter
from geospark.core.formatMapper.shapefileParser import ShapefileReader
spark = SparkSession.builder\
.config("spark.serializer", KryoSerializer.getName)\
.config("spark.kryo.registrator", GeoSparkKryoRegistrator.getName).\
getOrCreate()
GeoSparkRegistrator.registerAll(spark)
g2Df = spark.read.format("csv").option("sep","|").option("header","true")\
.load(r"D:\pycharm\pythonProject\lt_demo\inputData\20200630.txt")
g2Df.show()
# import sys
# sys.exit(0)
g2Df.createOrReplaceTempView("g2view")
#經(jīng)緯度不能為null湿酸,否則在轉(zhuǎn)換的時(shí)候就會(huì)報(bào)錯(cuò)
spark.sql("""select 'Point('||g2view.x_ray||' '||g2view.y_ray||')' as geom from g2view """)\
.filter(col("geom").isNotNull()).createOrReplaceTempView("g2_obj")
spark.sql("""select ST_PointFromText(g2_obj.geom,"WKT") from g2_obj""").show(truncate=False)
數(shù)據(jù)結(jié)果:
+----------------------------+
|st_pointfromtext(geom, WKT) |
+----------------------------+
|POINT (113.8557 23.11161) |
|POINT (114.18867 22.72796) |
|POINT (116.12976 23.44503) |
|POINT (112.49416 22.4743) |
|POINT (115.21093 22.89254) |
|POINT (110.79369 21.76382) |
|POINT (110.005343 21.091536)|
|POINT (110.999199 22.573701)|
|POINT (113.6505 22.92586) |
|POINT (113.19594 23.35344) |
|POINT (116.12976 23.44503) |
|POINT (113.487001 23.1598) |
|POINT (110.005343 21.091536)|
|POINT (113.143101 23.028711)|
|POINT (114.30605 25.11547) |
|POINT (113.36348 22.92292) |
|POINT (116.58298 23.42911) |
|POINT (115.97967 24.2775) |
|POINT (112.361328 22.806479)|
|POINT (113.143101 23.028711)|
+----------------------------+