JTS: 25 Index 索引

文章目录

版本

org.locationtech.jts:jts-core:1.19.0
链接: github

代码

在这里插入图片描述
在这里插入图片描述

package pers.stu.index;

import org.locationtech.jts.geom.*;
import org.locationtech.jts.index.hprtree.HPRtree;
import org.locationtech.jts.index.quadtree.Quadtree;
import org.locationtech.jts.index.strtree.STRtree;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.stu.util.GeoGebraUtil;
import pers.stu.util.SRIDOutUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 索引使用
 * @author LiHan
 * 2023-11-16 14:11:28
 */
public class IndexUse {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexUse.class);

    private final GeometryFactory geometryFactory = new GeometryFactory();

    private final WKTReader wktReader = new WKTReader();

    public static void main(String[] args) {
        IndexUse indexUse = new IndexUse();
        Geometry[] geometries = indexUse.test01();
        indexUse.test00(geometries);

    }


    public void test00(Geometry[] geometries) {
        Coordinate[] coordinates = new Coordinate[] {
                new Coordinate(10, 70), new Coordinate(130, 70), new Coordinate(130, 5), new Coordinate(10, 5),
                new Coordinate(10, 70)
        };
        Polygon polygon1 = geometryFactory.createPolygon(coordinates);

        STRtree stRtree = new STRtree();
        HPRtree hpRtree = new HPRtree();
        Quadtree quadtree = new Quadtree();

        for (Geometry geometry : geometries) {
            stRtree.insert(geometry.getEnvelopeInternal(), geometry);
            hpRtree.insert(geometry.getEnvelopeInternal(), geometry);
            quadtree.insert(geometry.getEnvelopeInternal(), geometry);
            LOGGER.info(SRIDOutUtil.ewkt(geometry));
        }

        // 查询范围内的点
        Coordinate[] coordinates1 = new Coordinate[] {
                new Coordinate(20, 60), new Coordinate(110,60), new Coordinate(110, 40), new Coordinate(20, 40),
                new Coordinate(20, 60)
        };
        Polygon polygon2 = geometryFactory.createPolygon(coordinates1);

        LOGGER.info(SRIDOutUtil.ewkt(polygon1));
        LOGGER.info(SRIDOutUtil.ewkt(polygon2));

        long startTime = System.currentTimeMillis();
        List<?> strings0 = stRtree.query(polygon2.getEnvelopeInternal());
        long endTime = System.currentTimeMillis();
        LOGGER.info("查询时间:{}, 查询出多少个:{}", (endTime - startTime), strings0.size());

        startTime = System.currentTimeMillis();
        List<?> strings1 = hpRtree.query(polygon2.getEnvelopeInternal());
        endTime = System.currentTimeMillis();
        LOGGER.info("查询时间:{}, 查询出多少个:{}", (endTime - startTime), strings1.size());

        startTime = System.currentTimeMillis();
        List<?> strings2 = quadtree.query(polygon2.getEnvelopeInternal());
        endTime = System.currentTimeMillis();
        LOGGER.info("查询时间:{}, 查询出多少个:{}", (endTime - startTime), strings2.size());

    }

    /**
     * 根据字符串生成点
     * @return
     */
    public Geometry[] test01() {
        String pointstr = "POINT (100.93320018996765 30.29943781543053), POINT (13.30741823728117 60.67274654095857), POINT (60.70677831219285 50.76468552433665), POINT (79.9774150875676 6.13769673816433), POINT (13.469139126248212 61.88187995248715), POINT (95.38412933841829 15.41536130253155), POINT (40.93550696399639 7.389429211019658), POINT (112.19185248633876 43.044841254913585), POINT (64.93098452078718 55.718223668244505), POINT (75.18707435725018 21.290767238084296), POINT (43.19181074516166 21.91744619247759), POINT (78.6998064478683 28.22619358146395), POINT (39.74310574754522 60.57609402289477), POINT (123.04662320725421 7.795508556020519), POINT (76.89786001207732 69.85942513105607), POINT (76.31742530446084 32.12544336011835), POINT (127.91395723412276 56.33513989770151), POINT (41.69518931757785 57.548561690027924), POINT (15.088371606675857 39.351053658116584), POINT (78.11503404106556 54.031172109566576), POINT (97.55968078955156 27.344179312843707), POINT (63.43623031922804 67.9563571044751), POINT (106.33241825308681 51.610539193132404), POINT (24.41792094776949 20.502891289217967), POINT (71.0288003863869 55.745698518145545), POINT (46.65649286165637 68.18206981769515), POINT (103.61568598003761 66.17793293203339), POINT (40.259762698526075 63.44704838697968), POINT (58.84673093355425 40.95909232919178), POINT (53.40030233311849 21.67306205974595), POINT (59.40192368071497 10.252649925535906), POINT (56.00743431476519 46.22190442393951), POINT (115.69886415064397 32.76407604079313), POINT (39.724593204999735 40.75041790075829), POINT (106.09234903836763 62.69561134190499), POINT (57.875954495843644 52.400176825801594), POINT (12.670980796267788 36.67565203171152), POINT (20.55344054035027 6.014439222117529), POINT (51.101754035293695 24.51239553914466), POINT (65.05231366905198 58.06838350181227), POINT (109.96572464501043 51.552050253992064), POINT (60.75088584041608 69.5700385080002), POINT (125.77005370989069 38.88381968247357), POINT (40.14694507590363 14.791752539472409), POINT (104.31392948686506 10.627053325212426), POINT (74.13074534976539 27.325914779581993), POINT (57.32804373115375 65.06035063927564), POINT (89.69158892963037 26.62364352581612), POINT (35.299481793575254 28.40809200114242), POINT (42.122194887866755 46.87143604224548), POINT (23.136091679836582 11.686749937311768), POINT (21.40227330375405 60.7883647593492), POINT (44.816541618210444 7.13154354007311), POINT (93.8098595608537 53.68511795811766), POINT (116.41454638112394 46.37336556272685), POINT (114.9262038079334 12.916295073271623), POINT (93.8778142071642 12.716617283444672), POINT (39.067169340753836 34.6786129127989), POINT (22.684582131403218 29.58046908789757), POINT (72.79127408804939 53.96022020132737), POINT (30.99266359846364 51.40924591968793), POINT (16.50172764138112 24.330128730165637), POINT (36.138250541078015 27.132051389942756), POINT (108.27696864312622 32.171790954042734), POINT (76.33378303732106 39.55738951743279), POINT (107.93143152642689 49.91114810249921), POINT (15.156473823335212 36.92548368283366), POINT (116.9254066529076 34.767006840955986), POINT (15.911209012467943 60.20914415917251), POINT (41.110118395108245 68.5603994685265), POINT (106.19374956859919 6.653434362401953), POINT (108.41126343996818 5.159624173707641), POINT (109.9723839094359 41.475084054485514), POINT (12.421466158003778 10.208940236582205), POINT (15.24828027098259 65.6044750230085), POINT (65.48933882378255 32.89142085857101), POINT (29.847076436066402 22.846220731012078), POINT (91.25990476111149 56.61030647549254), POINT (116.13634503215953 64.85875403797728), POINT (66.73850094400409 24.638180591711947), POINT (87.45584604095102 46.90900382369785), POINT (17.817284846221916 67.97959239182896), POINT (34.789568594442166 6.445170578269741), POINT (99.82078285700373 15.920350458705496), POINT (51.70084528089928 30.148755293393265), POINT (35.994073028546985 9.995386111730369), POINT (98.7939292683677 33.27642697721625), POINT (101.9743417074574 59.397246758791965), POINT (115.95769195568941 47.78295626815545), POINT (73.15379722795602 65.16847399900439), POINT (31.389197563433157 32.92839183149218), POINT (74.4590801307741 6.873717048064896), POINT (90.88717903817849 53.8157788226297), POINT (128.4395016999927 38.831200800399095), POINT (99.14758182019898 29.58016191619769), POINT (27.673638794083715 38.398187946587974), POINT (126.785535442564 23.755281404569164), POINT (81.37097901243874 26.251414799826428), POINT (112.55031646181288 19.68124222846002), POINT (23.66253380355625 13.080969413477463)";
        String[] strings = pointstr.split(", ");
        LOGGER.info(Arrays.toString(strings));
        Geometry[] geometries = new Geometry[strings.length];
        for (int i = 0; i < strings.length; i++) {
            try {
                geometries[i] = wktReader.read(strings[i]);
                LOGGER.info(GeoGebraUtil.compare(geometries[i]));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return geometries;
    }

    /**
     * 根据面生成随机点位
     * @param polygon 多边形
     * @param size 点的数量
     * @return 随机点数组
     */
    public Point[] test02(Polygon polygon, int size) {
        Envelope envelope = polygon.getEnvelopeInternal();

        // 获取 最大,最小XY
        double maxX = envelope.getMaxX();
        double minX = envelope.getMinX();
        double maxY = envelope.getMaxY();
        double minY = envelope.getMinY();

        Random random = new Random();
        Point[] points = new Point[size];
        for (int i = 0; i < size; i++) {
            double randomX1 = minX + (maxX - minX) * random.nextDouble();
            double randomY1 = minY + (maxY - minY) * random.nextDouble();
            Point point = geometryFactory.createPoint(new Coordinate(randomX1, randomY1));
            LOGGER.info(GeoGebraUtil.compare(point));
            points[i] = point;
        }

        return points;
    }
}

18:00:24.551 [main] INFO  pers.stu.index.IndexUse - 查询时间:1, 查询出多少个:22
18:00:24.553 [main] INFO  pers.stu.index.IndexUse - 查询时间:2, 查询出多少个:22
18:00:24.553 [main] INFO  pers.stu.index.IndexUse - 查询时间:0, 查询出多少个:35
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值