zhangfengcdt commented on code in PR #2831: URL: https://github.com/apache/sedona/pull/2831#discussion_r3111979503
########## common/src/main/java/org/apache/sedona/common/S2Geography/WkbS2Shape.java: ########## @@ -0,0 +1,267 @@ +/* + * 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.sedona.common.S2Geography; + +import com.google.common.geometry.S2; +import com.google.common.geometry.S2EdgeUtil; +import com.google.common.geometry.S2LatLng; +import com.google.common.geometry.S2Point; +import com.google.common.geometry.S2Predicates; +import com.google.common.geometry.S2Shape; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * An S2Shape implementation that reads WKB bytes once, converts all coordinates to S2Points in the + * constructor, and stores them in an array. This avoids constructing S2Loop/S2Polygon objects + * (which each build their own internal S2ShapeIndex), while also avoiding repeated trig calls on + * every getEdge() access. + * + * <p>Supports Point (type 1), LineString (type 2), and Polygon (type 3). Multi-types and + * collections should fall back to the full S2 Geography parse path. + */ +public class WkbS2Shape implements S2Shape { + + private final int dim; // S2 dimension: 0=point, 1=line, 2=polygon + private final S2Point[] vertices; // all vertices, pre-converted from WKB + private final int totalEdges; + private final int[] chainStarts; // edge offset for each chain + private final int[] chainLengths; // edge count for each chain + private final int[] vertexOffsets; // index into vertices[] for first vertex of each chain + + // For polygon containsOrigin — computed eagerly at construction for polygons + private final boolean containsOriginValue; + + public WkbS2Shape(byte[] wkb) { + boolean le = (wkb[0] == 0x01); + ByteBuffer buf = + ByteBuffer.wrap(wkb).order(le ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); + int wkbType = buf.getInt(1) & 0xFF; + Review Comment: this should be addressed now by using the same WKBReader.java pattern -- 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]
