2020-12-25

package com.xkzhangsan.time.converter;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Objects;

/**
 * 日期转换<br>
 * 包含Date、LocalDate、LocalDateTime、LocalTime、Instant、ZonedDateTime、YearMonth、Timestamp和long等互相转换<br>
 * 
 * 注意,ZonedDateTime相关的转换,尤其是其他时间转ZonedDateTime,要注意时间和对应时区一致。<br>
* @author xkzhangsan
*
 */
public class DateTimeConverterUtil {
	
	private DateTimeConverterUtil(){
	}

	/**
	 * LocalDateTime转Date
	 * @param localDateTime LocalDateTime
	 * @return Date
	 */
	public static Date toDate(LocalDateTime localDateTime) {
		Objects.requireNonNull(localDateTime, "localDateTime");
		return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
	}

	/**
	 * LocalDate转Date
	 * @param localDate LocalDate
	 * @return Date
	 */
	public static Date toDate(LocalDate localDate) {
		Objects.requireNonNull(localDate, "localDate");
		return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
	}
	
	/**
	 * LocalTime转Date
	 * 以当天的日期+LocalTime组成新的LocalDateTime转换为Date
	 * @param localTime LocalTime
	 * @return Date
	 */
	public static Date toDate(LocalTime localTime) {
		Objects.requireNonNull(localTime, "localTime");
		return Date.from(LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault()).toInstant());
	}	

	/**
	 * Instant转Date
	 * @param instant Instant
	 * @return Date
	 */
	public static Date toDate(Instant instant) {
		return Date.from(instant);
	}
	
	/**
	 * 时间戳epochMilli毫秒转Date
	 * @param epochMilli 时间戳
	 * @return Date
	 */
	public static Date toDate(long epochMilli){
		Objects.requireNonNull(epochMilli, "epochMilli");
		return new Date(epochMilli);
	}
	
	/**
	 * ZonedDateTime转Date
	 * 注意时间对应的时区和默认时区差异
	 * @param zonedDateTime ZonedDateTime
	 * @return Date
	 */
	public static Date toDate(ZonedDateTime zonedDateTime) {
		Objects.requireNonNull(zonedDateTime, "zonedDateTime");
		return Date.from(zonedDateTime.toInstant());
	}
	
	/**
	 * YearMonth转Date
	 * 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28
	 * 如果要转换为当月最后一天,可以使用下面方法:toDateEndOfMonth(YearMonth)
	 * @param yearMonth YearMonth
	 * @param dayOfMonth 天
	 * @return Date
	 */
	public static Date toDate(YearMonth yearMonth, int dayOfMonth) {
		Objects.requireNonNull(yearMonth, "yearMonth");
		return toDate(yearMonth.atDay(dayOfMonth));
	}
	
	/**
	 * YearMonth转Date,转换为当月第一天
	 * @param yearMonth YearMonth
	 * @return Date
	 */
	public static Date toDateStartOfMonth(YearMonth yearMonth) {
		return toDate(yearMonth, 1);
	}
	
	/**
	 * YearMonth转Date,转换为当月最后一天
	 * @param yearMonth YearMonth
	 * @return Date
	 */
	public static Date toDateEndOfMonth(YearMonth yearMonth) {
		Objects.requireNonNull(yearMonth, "yearMonth");
		return toDate(yearMonth.atEndOfMonth());
	}

	/**
	 * Date转LocalDateTime
	 * @param date Date
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(Date date) {
		Objects.requireNonNull(date, "date");
		return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
	}
	
	/**
	 * Timestamp转LocalDateTime
	 * @param timestamp Timestamp
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(Timestamp timestamp) {
		Objects.requireNonNull(timestamp, "timestamp");
		return timestamp.toLocalDateTime();
	}

	/**
	 * LocalDate转LocalDateTime
	 * @param localDate LocalDate
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(LocalDate localDate) {
		Objects.requireNonNull(localDate, "localDate");
		return localDate.atStartOfDay();
	}
	
	/**
	 * LocalTime转LocalDateTime
	 * 以当天的日期+LocalTime组成新的LocalDateTime
	 * @param localTime LocalTime
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(LocalTime localTime) {
		Objects.requireNonNull(localTime, "localTime");
		return LocalDate.now().atTime(localTime);
	}

	/**
	 * Instant转LocalDateTime
	 * @param instant Instant
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(Instant instant) {
		return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
	}
	
	/**
	 * 时间戳epochMilli毫秒转LocalDateTime
	 * @param epochMilli 时间戳
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(long epochMilli) {
		Objects.requireNonNull(epochMilli, "epochMilli");
		return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
	}
	
	/**
	 * temporal转LocalDateTime
	 * @param temporal TemporalAccessor
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(TemporalAccessor temporal) {
		return LocalDateTime.from(temporal);
	}
	
	/**
	 * ZonedDateTime转LocalDateTime
	 * 注意时间对应的时区和默认时区差异
	 * @param zonedDateTime ZonedDateTime
	 * @return LocalDateTime
	 */
	public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime) {
		Objects.requireNonNull(zonedDateTime, "zonedDateTime");
		return zonedDateTime.toLocalDateTime();
	}

	/**
	 * Date转LocalDate
	 * @param date Date
	 * @return LocalDate
	 */
	public static LocalDate toLocalDate(Date date) {
		return toLocalDateTime(date).toLocalDate();
	}

	/**
	 * LocalDateTime转LocalDate
	 * @param localDateTime LocalDateTime
	 * @return LocalDate
	 */
	public static LocalDate toLocalDate(LocalDateTime localDateTime) {
		Objects.requireNonNull(localDateTime, "localDateTime");
		return localDateTime.toLocalDate();
	}

	/**
	 * Instant转LocalDate
	 * @param instant Instant
	 * @return LocalDate
	 */
	public static LocalDate toLocalDate(Instant instant) {
		return toLocalDateTime(instant).toLocalDate();
	}
	
	/**
	 * 时间戳epochMilli毫秒转LocalDate
	 * @param epochMilli 时间戳
	 * @return LocalDate
	 */
	public static LocalDate toLocalDate(long epochMilli) {
		Objects.requireNonNull(epochMilli, "epochMilli");
		return toLocalDateTime(epochMilli).toLocalDate();
	}	
	
	/**
	 * temporal转LocalDate
	 * @param temporal TemporalAccessor
	 * @return LocalDate
	 */
	public static LocalDate toLocalDate(TemporalAccessor temporal) {
		return LocalDate.from(temporal);
	}
	
	/**
	 * ZonedDateTime转LocalDate
	 * 注意时间对应的时区和默认时区差异
	 * @param zonedDateTime ZonedDateTime
	 * @return LocalDate
	 */
	public static LocalDate toLocalDate(ZonedDateTime zonedDateTime) {
		Objects.requireNonNull(zonedDateTime, "zonedDateTime");
		return zonedDateTime.toLocalDate();
	}
	
	/**
	 * YearMonth转LocalDate
	 * 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28
	 * 如果要转换为当月最后一天,可以使用下面方法:toLocalDateEndOfMonth(YearMonth)
	 * @param yearMonth YearMonth
	 * @param dayOfMonth 天
	 * @return LocalDate
	 */
	public static LocalDate toLocalDate(YearMonth yearMonth, int dayOfMonth) {
		Objects.requireNonNull(yearMonth, "yearMonth");
		return yearMonth.atDay(dayOfMonth);
	}
	
	/**
	 * YearMonth转LocalDate,转换为当月第一天
	 * @param yearMonth YearMonth
	 * @return LocalDate
	 */
	public static LocalDate toLocalDateStartOfMonth(YearMonth yearMonth) {
		return toLocalDate(yearMonth, 1);
	}
	
	/**
	 * YearMonth转LocalDate,转换为当月最后一天
	 * @param yearMonth YearMonth
	 * @return LocalDate
	 */
	public static LocalDate toLocalDateEndOfMonth(YearMonth yearMonth) {
		Objects.requireNonNull(yearMonth, "yearMonth");
		return yearMonth.atEndOfMonth();
	}

	/**
	 * Date转LocalTime
	 * @param date Date
	 * @return LocalTime
	 */
	public static LocalTime toLocalTime(Date date) {
		return toLocalDateTime(date).toLocalTime();
	}

	/**
	 * LocalDateTime转LocalTime
	 * @param localDateTime LocalDateTime
	 * @return LocalTime
	 */
	public static LocalTime toLocalTime(LocalDateTime localDateTime) {
		Objects.requireNonNull(localDateTime, "localDateTime");
		return localDateTime.toLocalTime();
	}

	/**
	 * Instant转LocalTime
	 * @param instant Instant
	 * @return LocalTime
	 */
	public static LocalTime toLocalTime(Instant instant) {
		return toLocalDateTime(instant).toLocalTime();
	}
	
	/**
	 * temporal转LocalTime
	 * @param temporal TemporalAccessor
	 * @return LocalTime
	 */
	public static LocalTime toLocalTime(TemporalAccessor temporal) {
		return LocalTime.from(temporal);
	}
	
	/**
	 * ZonedDateTime转LocalTime
	 * 注意时间对应的时区和默认时区差异
	 * @param zonedDateTime ZonedDateTime
	 * @return LocalTime
	 */
	public static LocalTime toLocalTime(ZonedDateTime zonedDateTime) {
		Objects.requireNonNull(zonedDateTime, "zonedDateTime");
		return zonedDateTime.toLocalTime();
	}

	/**
	 * Date转Instant
	 * @param date Date
	 * @return Instant
	 */
	public static Instant toInstant(Date date) {
		Objects.requireNonNull(date, "date");
		return date.toInstant();
	}
	
	/**
	 * Timestamp转Instant
	 * @param timestamp Timestamp
	 * @return Instant
	 */
	public static Instant toInstant(Timestamp timestamp) {
		Objects.requireNonNull(timestamp, "timestamp");
		return timestamp.toInstant();
	}

	/**
	 * LocalDateTime转Instant
	 * @param localDateTime LocalDateTime
	 * @return Instant
	 */
	public static Instant toInstant(LocalDateTime localDateTime) {
		Objects.requireNonNull(localDateTime, "localDateTime");
		return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
	}

	/**
	 * LocalDate转Instant
	 * @param localDate LocalDate
	 * @return Instant
	 */
	public static Instant toInstant(LocalDate localDate) {
		return toLocalDateTime(localDate).atZone(ZoneId.systemDefault()).toInstant();
	}
	
	/**
	 * LocalTime转Instant
	 * 以当天的日期+LocalTime组成新的LocalDateTime转换为Instant
	 * @param localTime LocalTime
	 * @return Instant
	 */
	public static Instant toInstant(LocalTime localTime) {
		return toLocalDateTime(localTime).atZone(ZoneId.systemDefault()).toInstant();
	}
	
	/**
	 * 时间戳epochMilli毫秒转Instant
	 * @param epochMilli 时间戳
	 * @return Instant
	 */
	public static Instant toInstant(long epochMilli) {
		Objects.requireNonNull(epochMilli, "epochMilli");
		return Instant.ofEpochMilli(epochMilli);
	}
	
	/**
	 * temporal转Instant
	 * @param temporal TemporalAccessor
	 * @return Instant
	 */
	public static Instant toInstant(TemporalAccessor temporal) {
		return Instant.from(temporal);
	}
	
	/**
	 * ZonedDateTime转Instant
	 * 注意,zonedDateTime时区必须和当前系统时区一致,不然会出现问题
	 * @param zonedDateTime ZonedDateTime
	 * @return Instant
	 */
	public static Instant toInstant(ZonedDateTime zonedDateTime) {
		Objects.requireNonNull(zonedDateTime, "zonedDateTime");
		return zonedDateTime.toInstant();
	}
	
	/**
	 * Date转时间戳
	 * 从1970-01-01T00:00:00Z开始的毫秒值
	 * @param date Date
	 * @return 时间戳
	 */
	public static long toEpochMilli(Date date){
		Objects.requireNonNull(date, "date");
		return date.getTime();
	}
	
	/**
	 * Timestamp转时间戳
	 * 从1970-01-01T00:00:00Z开始的毫秒值
	 * @param timestamp Timestamp
	 * @return 时间戳
	 */
	public static long toEpochMilli(Timestamp timestamp){
		Objects.requireNonNull(timestamp, "timestamp");
		return timestamp.getTime();
	}
	
	/**
	 * LocalDateTime转时间戳
	 * 从1970-01-01T00:00:00Z开始的毫秒值
	 * @param localDateTime LocalDateTime
	 * @return 时间戳
	 */
	public static long toEpochMilli(LocalDateTime localDateTime){
		return toInstant(localDateTime).toEpochMilli();
	}
	
	/**
	 * LocalDate转时间戳
	 * 从1970-01-01T00:00:00Z开始的毫秒值
	 * @param localDate LocalDate
	 * @return 时间戳
	 */
	public static long toEpochMilli(LocalDate localDate){
		return toInstant(localDate).toEpochMilli();
	}
	
	/**
	 * Instant转时间戳
	 * 从1970-01-01T00:00:00Z开始的毫秒值
	 * @param instant Instant
	 * @return 时间戳
	 */
	public static long toEpochMilli(Instant instant){
		Objects.requireNonNull(instant, "instant");
		return instant.toEpochMilli();
	}
	
	/**
	 * ZonedDateTime转时间戳,注意,zonedDateTime时区必须和当前系统时区一致,不然会出现问题
	 * 从1970-01-01T00:00:00Z开始的毫秒值
	 * @param zonedDateTime ZonedDateTime
	 * @return 时间戳
	 */
	public static long toEpochMilli(ZonedDateTime zonedDateTime) {
		Objects.requireNonNull(zonedDateTime, "zonedDateTime");
		return zonedDateTime.toInstant().toEpochMilli();
	}
	
	/**
	 * Date转ZonedDateTime,时区为系统默认时区
	 * @param date Date
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(Date date) {
		Objects.requireNonNull(date, "date");
		return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault());
	}
	
	/**
	 * Date转ZonedDateTime
	 * @param date Date
	 * @param zoneId 目标时区
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(Date date, String zoneId) {
		Objects.requireNonNull(zoneId, "zoneId");
		return toZonedDateTime(date, ZoneId.of(zoneId));
	}
	
	/**
	 * Date转ZonedDateTime
	 * @param date Date
	 * @param zone 目标时区
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(Date date, ZoneId zone) {
		Objects.requireNonNull(date, "date");
		Objects.requireNonNull(zone, "zone");
		return Instant.ofEpochMilli(date.getTime()).atZone(zone);
	}
	
	/**
	 * LocalDateTime转ZonedDateTime,时区为系统默认时区
	 * @param localDateTime LocalDateTime
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime) {
		Objects.requireNonNull(localDateTime, "localDateTime");
		return localDateTime.atZone(ZoneId.systemDefault());
	}
	
	/**
	 * LocalDateTime转ZonedDateTime,时区为zoneId对应时区
	 * 注意,需要保证localDateTime和zoneId是对应的,不然会出现错误
	 * 
	 * @param localDateTime LocalDateTime
	 * @param zoneId LocalDateTime
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime, String zoneId) {
		Objects.requireNonNull(localDateTime, "localDateTime");
		Objects.requireNonNull(zoneId, "zoneId");
		return localDateTime.atZone(ZoneId.of(zoneId));
	}	

	/**
	 * LocalDate转ZonedDateTime,时区为系统默认时区
	 * @param localDate LocalDate
	 * @return ZonedDateTime such as 2020-02-19T00:00+08:00[Asia/Shanghai]
	 */
	public static ZonedDateTime toZonedDateTime(LocalDate localDate) {
		Objects.requireNonNull(localDate, "localDate");
		return localDate.atStartOfDay().atZone(ZoneId.systemDefault());
	}
	
	/**
	 * LocalTime转ZonedDateTime
	 * 以当天的日期+LocalTime组成新的ZonedDateTime,时区为系统默认时区
	 * @param localTime LocalTime
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(LocalTime localTime) {
		Objects.requireNonNull(localTime, "localTime");
		return LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault());
	}

	/**
	 * Instant转ZonedDateTime,时区为系统默认时区
	 * @param instant Instant
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(Instant instant) {
		return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).atZone(ZoneId.systemDefault());
	}
	
	/**
	 * 时间戳epochMilli毫秒转ZonedDateTime,时区为系统默认时区
	 * @param epochMilli 时间戳
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(long epochMilli) {
		Objects.requireNonNull(epochMilli, "epochMilli");
		return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault())
				.atZone(ZoneId.systemDefault());
	}
	
	/**
	 * temporal转ZonedDateTime,时区为系统默认时区
	 * @param temporal TemporalAccessor
	 * @return ZonedDateTime
	 */
	public static ZonedDateTime toZonedDateTime(TemporalAccessor temporal) {
		return LocalDateTime.from(temporal).atZone(ZoneId.systemDefault());
	}
	
	/**
	 * Date转YearMonth
	 * @param date Date
	 * @return YearMonth
	 */
	public static YearMonth toYearMonth(Date date){
		LocalDate localDate = toLocalDate(date);
		return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
	}
	
	/**
	 * LocalDateTime转YearMonth
	 * @param localDateTime LocalDateTime
	 * @return YearMonth
	 */
	public static YearMonth toYearMonth(LocalDateTime localDateTime){
		LocalDate localDate = toLocalDate(localDateTime);
		return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
	}
	
	/**
	 * LocalDate转YearMonth
	 * @param localDate LocalDate
	 * @return YearMonth
	 */
	public static YearMonth toYearMonth(LocalDate localDate){
		Objects.requireNonNull(localDate, "localDate");
		return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
	}
	
	/**
	 * Instant转YearMonth
	 * @param instant Instant
	 * @return YearMonth
	 */
	public static YearMonth toYearMonth(Instant instant){
		LocalDate localDate = toLocalDate(instant);
		return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
	}
	
	/**
	 * ZonedDateTime转YearMonth
	 * @param zonedDateTime ZonedDateTime
	 * @return YearMonth
	 */
	public static YearMonth toYearMonth(ZonedDateTime zonedDateTime){
		LocalDate localDate = toLocalDate(zonedDateTime);
		return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
	}
	
	/**
	 * Date转Timestamp
	 * @param date Date
	 * @return Timestamp
	 */
	public static Timestamp toTimestamp(Date date){
		Objects.requireNonNull(date, "date");
		return new Timestamp(date.getTime());
	}
	
	/**
	 * LocalDateTime转Timestamp
	 * @param localDateTime LocalDateTime
	 * @return Timestamp
	 */
	public static Timestamp toTimestamp(LocalDateTime localDateTime){
		Objects.requireNonNull(localDateTime, "localDateTime");
		return Timestamp.valueOf(localDateTime);
	}
	
	/**
	 * Instant转Timestamp
	 * @param instant Instant
	 * @return Timestamp
	 */
	public static Timestamp toTimestamp(Instant instant){
		Objects.requireNonNull(instant, "instant");
		return Timestamp.from(instant);
	}

	/**
	 * 时间戳epochMilli转Timestamp
	 * @param epochMilli 时间戳
	 * @return Timestamp
	 */
	public static Timestamp toTimestamp(long epochMilli){
		return new Timestamp(epochMilli);
	}
}

 

感谢你提供的列名信息,我们可以看到: - `"...1"`:可能是 Excel 中第一列无标题的自动命名(比如行号或空列),可以忽略。 - `"STATION"`:站点编号 - `"NAME"`:站点名称 - `"LATITUDE"`:纬度 - `"LONGITUDE"`:经度 - `"ELEVATION"`:海拔 - `"statement"`:可能是一个状态或注释字段 - 后续从 `"2020-01-01"` 到 `"2020-01-31"` 是每日风速数据(共31天) --- ### ✅ 目标更新: 你要做的是:**根据给定的 SHP 范围裁剪站点,保留落在该地理范围内的所有站点及其完整的逐日风速数据。** 下面是适配你实际列名的完整 R 语言代码,并处理好坐标、投影和数据结构问题。 ```r # 加载所需包 library(sf) library(readxl) library(dplyr) # ------------------- 参数设置 ------------------- excel_file <- "your_wind_station_data.xlsx" # 替换为你的实际文件路径 sheet_name <- "Sheet1" # 替换为你的工作表名 shp_file <- "your_boundary.shp" # 替换为你的SHP文件路径 # ------------------- 步骤1: 读取Excel数据 ------------------- df <- read_excel(excel_file, sheet = sheet_name) # 查看列名确认 names(df) <- make.names(names(df)) # 确保列名是合法的(防止空格等问题) cat("原始列名:\n"); print(names(df)) # ------------------- 步骤2: 提取空间信息并创建sf对象 ------------------- # 使用 LATITUDE 和 LONGITUDE 创建空间点(注意:顺序是 LON, LAT) stations_sf <- st_as_sf(df, coords = c("LONGITUDE", "LATITUDE"), # 经度在前,纬度在后 crs = 4326, # WGS84 地理坐标系 dim = "XY") # ------------------- 步骤3: 读取SHP边界并确保其CRS ------------------- boundary <- st_read(shp_file) # 如果boundary不是投影坐标系(如EPSG:32649),则需要检查并转换 # 假设你知道目标投影是 UTM Zone 49N (EPSG:32649),我们统一到这个坐标系进行空间操作 if (is.na(st_crs(boundary))) { stop("SHP文件没有坐标系信息,请先定义正确的CRS!") } # 将站点数据重投影到与SHP相同的坐标系下进行空间判断 stations_projected <- st_transform(stations_sf, crs = st_crs(boundary)) # ------------------- 步骤4: 空间裁剪 —— 找出在SHP范围内的站点 ------------------- # 使用 st_intersects 或 st_within 进行空间子集提取 # 这里使用 [s,t] 语法:返回落在任意多边形内的站点 stations_clipped <- stations_projected[boundary, , op = st_intersects] # 若你想更严格地要求“完全包含”,可用 st_within,但通常 st_intersects 更通用 # ------------------- 步骤5: 转回原始经纬度并提取属性表格 ------------------- # 将结果转回WGS84以便保留原始经纬度格式输出 stations_wgs84 <- st_transform(stations_clipped, crs = 4326) # 去掉geometry列,恢复为普通data.frame,同时保留原始所有列(包括每日数据) result_df <- st_drop_geometry(stations_wgs84) # 可选:重新排序列,把时间序列放在后面 date_cols <- grep("^\\d{4}-\\d{2}-\\d{2}", names(result_df), value = TRUE) non_date_cols <- setdiff(names(result_df), date_cols) final_df <- select(result_df, c(non_date_cols, date_cols)) # 按逻辑排序 # ------------------- 步骤6: 导出结果 ------------------- write.csv(final_df, "clipped_stations_202001.csv", row.names = FALSE, na = "") cat("共保留了", nrow(final_df), "个站点在SHP范围内。\n") ``` --- ### ✅ 关键说明: | 功能 | 说明 | |------|------| | `coords = c("LONGITUDE", "LATITUDE")` | 必须是 **经度在前,纬度在后**,否则位置错误! | | `crs = 4326` | 表示输入的经纬度使用 WGS84 坐标系 | | `st_transform(...)` | 将点从地理坐标(度)转为投影坐标(米),确保与 SHP 在同一空间参考下比较 | | `st_intersects` | 判断点是否与多边形相交(即落在内部),适用于大多数情况 | | `st_drop_geometry()` | 移除空间结构,得到纯数据框用于导出 | > 💡 输出的 CSV 文件将包含原始所有列,包括 `STATION`, `NAME`, `LATITUDE`, `LONGITUDE`, `ELEVATION`, `statement` 和所有日期列(如 `2020-01-01` 等),仅保留位于 SHP 范围内的站点。 --- ### ✅ 示例输出片段(final_df 头几行): ``` ...1 STATION NAME LATITUDE LONGITUDE ELEVATION statement 2020-01-01 2020-01-02 ... 1 1 101 Beijing 39.90 116.4 50.0 good 3.2 4.1 2 2 102 Tianjin 39.08 117.2 10.0 good 5.0 3.8 ... ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值