由于hbase的filter 没有not, 一般判断两个矩形相交的基本算法如下:
!(XMin > other.XMax || YMin > other.YMax || XMax < other.XMin || YMax < other.YMin)
改成and之后:
XMin< other.XMax && YMin < other.YMax && XMax >other.XMin && YMax > other.YMin
filter写法:
static FilterList IntersectFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.LESS, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.LESS, Bytes.toBytes(ymax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.GREATER, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.GREATER, Bytes.toBytes(ymin)));
return filters1;
}
static FilterList WithInFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.LESS, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.LESS, Bytes.toBytes(ymin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.GREATER, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.GREATER, Bytes.toBytes(ymax)));
return filters1;
}
static FilterList ContiansFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.GREATER, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.GREATER, Bytes.toBytes(ymin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.LESS, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.LESS, Bytes.toBytes(ymax)));
return filters1;
}
static FilterList DisjoinFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ONE);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.GREATER, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.GREATER, Bytes.toBytes(ymax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.LESS, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.LESS, Bytes.toBytes(ymin)));
return filters1;
}
我将空间数据存到列簇GOEMTRY中, 其中包含列(Geometry, xmin,ymin,xmax,ymax), 当然可以将 xmin,ymin,xmax,ymax编码存储到rowkey, 但是目前还没研究到这一步, 目前先按照传统数据库存储, 然后将数据按照GeoHash, zordering ,xzordering 编码,? 最后查询得时候给scan1.withStartRow(start, true);?scan1.withStopRow(end, true);? 这样可以极大加快查询.
此次测验代码同普通得关系数据库已做同步比较, 查询数据量和数据上同步正确,
?
其中GsKernel为自主研发组件, 用于空间数据读写得,? 开源替代方案为jts,gdal 即可完成常见shp空间数据得空间查询.
具体得代码如下
package hbasedatabase;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.Cell.Type;
import org.apache.hadoop.hbase.CellBuilder;
import org.apache.hadoop.hbase.CellBuilderFactory;
import org.apache.hadoop.hbase.CellBuilderType;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
import com.geostar.*;
import com.geostar.kernel.GsAny;
import com.geostar.kernel.GsBox;
import com.geostar.kernel.GsBox3D;
import com.geostar.kernel.GsConnectProperty;
import com.geostar.kernel.GsEndian;
import com.geostar.kernel.GsEnvelope;
import com.geostar.kernel.GsFeature;
import com.geostar.kernel.GsFeatureClass;
import com.geostar.kernel.GsFeatureCursor;
import com.geostar.kernel.GsField;
import com.geostar.kernel.GsFieldType;
import com.geostar.kernel.GsFieldVector;
import com.geostar.kernel.GsFields;
import com.geostar.kernel.GsGeoDatabase;
import com.geostar.kernel.GsGeohash;
import com.geostar.kernel.GsGeometry;
import com.geostar.kernel.GsGeometryBlob;
import com.geostar.kernel.GsGeometryFactory;
import com.geostar.kernel.GsSpatialQueryFilter;
import com.geostar.kernel.GsSqliteGeoDatabaseFactory;
import com.geostar.kernel.GsWKTOGCWriter;
import jnr.ffi.Struct.int16_t;
public class featurekey {
private static final String TABLE_NAME = "BOU2_4M_S";
static GsFeatureClass pfcsClass = null;
static GsGeohash ghGeohash = null;// new GsGeohash();
static Configuration conf = null;
static TableName tableName = null;
static Table table = null;
static Connection connection = null;
static Admin admin = null;
// 测试数据
static String familygeos[] = { "GEOMETRY", "INFO" };
static String family1[] = { "GEOMETRY", "XMIN", "YMIN", "XMAX", "YMAX" };
static String family1FieldsType[] = { "eGeometryType", "eDoubleType", "eDoubleType", "eDoubleType", "eDoubleType" };
static String family2[] = {};
static GsFieldType family2FieldsType[] = {};
static HashMap<String, HashMap<String, String>> map = new HashMap<String, HashMap<String, String>>();
static String polygon1 = "POLYGON((113.4854749254902 23.41916473613878,113.4854365755001 23.41911314486015,113.485408828651 23.4190774788114,113.485424543505 23.4190619093373,113.485437580626 23.41905590278759,113.485467041062 23.41905302022758,113.4854947123682 23.41905762592069,113.4855127324191 23.41905317072354,113.485522569163 23.41903956918361,113.485519427731 23.41901698699501,113.485513058565 23.41896884002222,113.48549513176 23.41894918873771,113.4854789845281 23.41892502695794,113.4854545713212 23.4188872952044,113.4854433523681 23.41882847882044,113.485446765259 23.418805917317,113.485468060442 23.41878946421219,113.4854909273421 23.41877139194617,113.4855138857942 23.4187549443131,113.4855334478511 23.41875654278465,113.485538299341 23.41876703341984,113.4855366755932 23.41878210576323,113.485525187459 23.41879263287558,113.4855169165271 23.41880614837947,113.485520208168 23.41881518831894,113.4855348373772 23.41882733549164,113.485543056915 23.41882736256224,113.4855544462291 23.418816836026,113.4855659050261 23.41881389506329,113.48557410711 23.41881843572094,113.485574066609 23.41882890934147,113.485569121258 23.41884243670313,113.4855641765311 23.41885605526005,113.485569039556 23.41886356452403,113.485578801144 23.4188696461664,113.485586944281 23.41886362348697,113.485614800673 23.41884565702208,113.4856361192332 23.41882315447521,113.4856492242071 23.41882464228223,113.485655657276 23.41883071386314,113.485655622366 23.41883974284661,113.4856539996091 23.41885481519478,113.485647421913 23.41886084393514,113.485624474005 23.41887431111695,113.485617878225 23.41888476315194,113.485612945372 23.41889531185609,113.485619477268 23.41890138195799,113.4856292553351 23.41890295000559,113.4856424017911 23.41889396419595,113.485657136101 23.41887893321473,113.4856702516191 23.41887744235538,113.4856816804711 23.41888199453445,113.485686537566 23.41889103962461,113.4856831421712 23.41890908573907,113.4856732649561 23.41893316181336,113.4856714794992 23.41896475687594,113.485681255294 23.41899196755162,113.485695890825 23.41900248858968,113.485710521049 23.41901463574959,113.4857170124572 23.41903118127403,113.485716954169 23.41904625877311,113.485712021321 23.41905680657749,113.4857054196 23.41906879444582,113.4857086015521 23.41908090391354,113.4857200488541 23.41908094160164,113.485731553472 23.4190658999807,113.485743019203 23.41906142317885,113.4857527857691 23.41906596988031,113.4857510866041 23.41907499338493,113.485749463848 23.41909006753929,113.485750987479 23.41910063569719,113.4857640990451 23.41910067886107,113.485775546349 23.41910071654525,113.485786924826 23.41911881310114,113.4857982268991 23.4191308581033,113.4857966041511 23.41914593135545,113.485791700618 23.41914889571933,113.485775342868 23.41915335642045,113.4857621739923 23.41916839075992,113.4857654248191 23.41918799550729,113.485780089345 23.41919102248164,113.4857979763121 23.41919568710591,113.485806148478 23.4192077227072,113.4858141645242 23.41923483638812,113.4858091908482 23.41925594720254,113.4857847017961 23.41926336164369,113.4857471021451 23.4192707311139,113.4856834370621 23.41926302825902,113.485660571405 23.41925545791713,113.4856411209191 23.4192252375878,113.4856055152551 23.41914683715942,113.485579566833 23.41910151683078,113.485550092563 23.41910746835663,113.485509195003 23.4191329762985,113.4854749254902 23.41916473613878))";
static HashMap<String, byte[]> valuesArrayList = new HashMap<String, byte[]>();
static void InitConf() throws IOException, ParseException {
System.loadLibrary("gsjavaport");
com.geostar.kernel.GsKernel.Initialize();
System.out.println("GsKernel loadlibrary succeed");
Configuration cnf = new Configuration();
cnf.set("hbase.zookeeper.quorum", "127.0.0.1:2181");
cnf.set("hbase.zookeeper.property.clientPort", "2181");
conf = HBaseConfiguration.create(cnf);
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
tableName = TableName.valueOf(TABLE_NAME);
table = connection.getTable(tableName);
InitFeatureData();
}
static void InitFeatureData() throws ParseException {
GsSqliteGeoDatabaseFactory factory = new GsSqliteGeoDatabaseFactory();
GsConnectProperty connectProperty = new GsConnectProperty("D:\\source\\kernel\\testdata\\400sqlite");
GsGeoDatabase pDatabase = factory.Open(connectProperty);
pfcsClass = pDatabase.OpenFeatureClass(TABLE_NAME);
ghGeohash = new GsGeohash();
HashMap<String, String> family1t = new HashMap<String, String>();
HashMap<String, String> family2t = new HashMap<String, String>();
for (int i = 0; i < family1.length; i++) {
family1t.put(family1[i], family1FieldsType[i]);
}
GsFields fdsFields = pfcsClass.Fields();
GsFieldVector fdsFieldVector = fdsFields.getFields();
for (int i = 0; i < fdsFieldVector.size(); i++) {
GsField fdField = fdsFieldVector.get(i);
String na = fdField.getName();
if (na.equals("GEOMETRY"))
continue;
family2t.put(fdField.getName(), fdField.getType().toString());
}
map.put(familygeos[0], family1t);
map.put(familygeos[1], family2t);
}
static void Exit() throws IOException {
// admin.disableTable(tableName);
// admin.close();
// table.close();
connection.close();
}
public static void main(String... args) throws IOException, ParseException {
InitConf();
NewVersionTestCreateTable();
Inserttest();
long begintime1 = System.currentTimeMillis();
SearchData(108, 20, 115, 26);
System.out.println("SearchData(108,20,115,26); " + (System.currentTimeMillis() - begintime1) + " ms");
begintime1 = System.currentTimeMillis();
SearchData(108, 25, 115, 27);
System.out.println(" SearchData(108,25,115,27); " + (System.currentTimeMillis() - begintime1) + " ms");
Exit();
}
public static void NewVersionTestCreateTable() throws IOException {
if (admin.tableExists(tableName)) {
if (!admin.isTableDisabled(tableName))
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("Table is exist. delete and create it");
// return;
}
// 创建表
TableDescriptorBuilder tbuilder = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptor familygeo = ColumnFamilyDescriptorBuilder.newBuilder(familygeos[0].getBytes()).build();
ColumnFamilyDescriptor familyinfo = ColumnFamilyDescriptorBuilder.newBuilder(familygeos[1].getBytes()).build();
List<ColumnFamilyDescriptor> families = new ArrayList<>();
families.add(familygeo);
families.add(familyinfo);
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName).setColumnFamilies(families)
.build();
admin.createTable(tableDescriptor);
}
public static void Inserttest() throws IOException {
if (!admin.tableExists(tableName)) {
System.out.println("Table does not exist.");
System.exit(-1);
}
table = connection.getTable(tableName);
CellBuilder cellbuilder = CellBuilderFactory.create(CellBuilderType.DEEP_COPY);
GsSqliteGeoDatabaseFactory factory = new GsSqliteGeoDatabaseFactory();
GsFeatureCursor pCursor = pfcsClass.Search();
GsFeature pFeature = pCursor.Next();
ArrayList<Put> arrayList = new ArrayList<Put>();
int nCommiT = 0;
int RegionServerCount = 9;
GsFields fdsFields = pfcsClass.Fields();
GsFieldVector fdsFieldVector = fdsFields.getFields();
do {
if (pFeature == null)
break;
// 这里
int partl = (int) (pFeature.OID() % ((long) RegionServerCount));
GsGeometry pGeometry = pFeature.Geometry();
GsBox3D box3d = pGeometry.Envelope();
String minString = ghGeohash.Forward(box3d.getXMin(), box3d.getYMin(), 18);
String maxString = ghGeohash.Forward(box3d.getXMax(), box3d.getYMax(), 18);
byte[] rowkey = Bytes.toBytes(minString + maxString + pFeature.OID());
Put put = new Put(rowkey);
// 写几何
int buffersize = (int) pGeometry.GeometryBlobPtr().BufferSize();
byte[] bBlob = new byte[buffersize];
pGeometry.GeometryBlobPtr().CopyToArray(bBlob, bBlob.length);
for (int i = 0; i < family1.length; i++) {
cellbuilder.setRow(rowkey);
cellbuilder.setFamily(Bytes.toBytes("GEOMETRY"));
if (0 == i) {
cellbuilder.setQualifier(Bytes.toBytes(family1[i]));
cellbuilder.setValue(bBlob);
} else {
cellbuilder.setQualifier(Bytes.toBytes(family1[i]));
switch (i) {
case 1:
cellbuilder.setValue(Bytes.toBytes(box3d.getXMin()));
break;
case 2:
cellbuilder.setValue(Bytes.toBytes(box3d.getYMin()));
break;
case 3:
cellbuilder.setValue(Bytes.toBytes(box3d.getXMax()));
break;
case 4:
cellbuilder.setValue(Bytes.toBytes(box3d.getYMax()));
break;
default:
break;
}
}
cellbuilder.setType(Type.Put);
Cell cl = cellbuilder.build();
put.add(cl);
}
// 写属性
for (Map.Entry<String, HashMap<String, String>> kvEntry : map.entrySet()) {
HashMap<String, String> vvkv = kvEntry.getValue();
String keyString = kvEntry.getKey();
if (keyString.equals("GEOMETRY"))
continue;
for (Map.Entry<String, String> kss : vvkv.entrySet()) {
String colmunString = kss.getKey();
int index = fdsFields.FindField(colmunString);
GsFieldType type = fdsFieldVector.get(index).getType();
cellbuilder.setRow(rowkey);
cellbuilder.setFamily(Bytes.toBytes(keyString));
cellbuilder.setQualifier(Bytes.toBytes(colmunString));
cellbuilder.setType(Type.Put);
switch (type) {
case eErrorType:
break;
/// \brief BOOL类型
case eBoolType:
cellbuilder.setValue(Bytes.toBytes(pFeature.ValueInt(index)));
break;
/// \brief 32位的整型
case eIntType:
case eUIntType:
cellbuilder.setValue(Bytes.toBytes(pFeature.ValueInt(index)));
break;
/// \brief 64位的整型
case eInt64Type:
case eUInt64Type:
cellbuilder.setValue(Bytes.toBytes(pFeature.ValueInt64(index)));
break;
/// \brief 字符串类型
case eStringType:
cellbuilder.setValue(Bytes.toBytes(pFeature.ValueString(index)));
break;
/// \brief 二进制类型
case eBlobType:
GsAny anyblob = pFeature.ValueBlob(index);
byte[] bBlob1 = new byte[anyblob.ValueSize()];
anyblob.AsBlob(bBlob1);
cellbuilder.setValue(bBlob1);
break;
/// \brief 浮点型
case eFloatType:
cellbuilder.setValue(Bytes.toBytes(pFeature.ValueFloat(index)));
break;
/// \brief 双精度浮点型
case eDoubleType:
cellbuilder.setValue(Bytes.toBytes(pFeature.ValueDouble(index)));
break;
/// \brief 几何类型
case eGeometryType:
break;
/// \brief 日期类型
case eDateType:
cellbuilder.setValue(Bytes.toBytes(pFeature.ValueDateTime(index).getTime()));
break;
default:
break;
}
Cell cl = cellbuilder.build();
put.add(cl);
}
}
arrayList.add(put);
if (nCommiT > 1000) {
// 插入数据
table.put(arrayList);
System.out.println("Insert nCommiT-" + nCommiT);
nCommiT = 0;
arrayList.clear();
}
} while (pCursor.Next(pFeature));
// 插入数据
table.put(arrayList);
System.out.println("Insert end");
}
static FilterList WithInFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.LESS, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.LESS, Bytes.toBytes(ymin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.GREATER, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.GREATER, Bytes.toBytes(ymax)));
return filters1;
}
static FilterList ContiansFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.GREATER, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.GREATER, Bytes.toBytes(ymin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.LESS, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.LESS, Bytes.toBytes(ymax)));
return filters1;
}
static FilterList IntersectFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.LESS, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.LESS, Bytes.toBytes(ymax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.GREATER, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.GREATER, Bytes.toBytes(ymin)));
return filters1;
}
static FilterList DisjoinFilter(double xmin, double ymin, double xmax, double ymax) {
FilterList filters1 = new FilterList(FilterList.Operator.MUST_PASS_ONE);
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMIN"),
CompareOperator.GREATER, Bytes.toBytes(xmax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMIN"),
CompareOperator.GREATER, Bytes.toBytes(ymax)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("XMAX"),
CompareOperator.LESS, Bytes.toBytes(xmin)));
filters1.addFilter(new SingleColumnValueFilter(Bytes.toBytes("GEOMETRY"), Bytes.toBytes("YMAX"),
CompareOperator.LESS, Bytes.toBytes(ymin)));
return filters1;
}
public static void SearchData(double xmin, double ymin, double xmax, double ymax)
throws IOException, ParseException {
if (!admin.tableExists(tableName)) {
System.out.println("Table does not exist.");
System.exit(-1);
}
table = connection.getTable(tableName);
Scan scan1 = new Scan();
// Bytes bytes = new Bytes();
// String fString = ghGeohash.Forward(xmin, ymin, 18);
// byte[] start = Bytes.add(Bytes.toBytes(fString), Bytes.toBytes(Integer.MIN_VALUE));
// Bytes bytesend = new Bytes();
// String feString = ghGeohash.Forward(xmax, ymax, 18);
// byte[] end = Bytes.add(Bytes.toBytes(feString), Bytes.toBytes(Integer.MAX_VALUE));
// scan1.withStartRow(start, true);
// scan1.withStopRow(end, true);
scan1.setFilter(IntersectFilter(xmin, ymin, xmax, ymax));
ResultScanner scanner1 = table.getScanner(scan1);
// 打印行的值
int ncount = 0;
for (org.apache.hadoop.hbase.client.Result res : scanner1) {
printrow(res, false);
ncount++;
}
// 关闭释放资源
scanner1.close();
System.out.println("search end ,get" + ncount + " features");
GsBox box = new GsBox(xmin, ymin, xmax, ymax);
GsSpatialQueryFilter pFilter = new GsSpatialQueryFilter(new GsEnvelope(box));
pFilter.FilterType("ANYINTERACT");
long n = pfcsClass.FeatureCount(pFilter);
System.out.println("GsKernel search end ,get" + n + " features");
}
static WKBReader wkbReader = new WKBReader();
static WKTWriter wktWriter = new WKTWriter();
static GsWKTOGCWriter gskwtWriter = null;
static void printrow(org.apache.hadoop.hbase.client.Result rtResult, boolean print)
throws ParseException, UnsupportedEncodingException {
if (null == gskwtWriter) {
gskwtWriter = new GsWKTOGCWriter();
}
// System.out.println(rtResult);
// System.out.println(new String(rtResult.getRow()).toString());
for (Map.Entry<String, HashMap<String, String>> kvEntry : map.entrySet()) {
{
HashMap<String, String> vvkv = kvEntry.getValue();
String keyString = kvEntry.getKey();
for (Map.Entry<String, String> kss : vvkv.entrySet()) {
String typeString = kss.getValue();
String colmunString = kss.getKey();
Cell lengthCells = rtResult.getColumnLatestCell(Bytes.toBytes(keyString),
Bytes.toBytes(colmunString));
if (null == lengthCells)
continue;
switch (typeString) {
case "eGeometryType":
// byte[] vv = lengthCells.getValueArray();
if (print) {
int offset = lengthCells.getValueOffset();
int len = lengthCells.getValueLength();
byte[] vvv = Arrays.copyOfRange(lengthCells.getValueArray(), offset, len);
GsGeometryBlob blob = new GsGeometryBlob();
blob.Copy(vvv, len);
GsGeometry pGeometry = GsGeometryFactory.CreateGeometryFromBlob(blob);
gskwtWriter.Write(new GsEnvelope(pGeometry.Envelope()));
String geometrystr = gskwtWriter.WKT();
System.out.println(colmunString + "--" + geometrystr);
}
break;
case "eDoubleType":
byte[] v = lengthCells.getValueArray();
if (print)
System.out.println(colmunString + "--"
+ String.format("%.8f", (Bytes.toDouble(v, lengthCells.getValueOffset()))));
break;
case "eInt64Type":
case "eUInt64Type":
byte[] v1 = lengthCells.getValueArray();
if (print)
System.out.println(colmunString + "--"
+ (Bytes.toLong(v1, lengthCells.getValueOffset(), lengthCells.getValueLength())));
break;
case "eStringType":
byte[] v11 = lengthCells.getValueArray();
if (print)
System.out.println(colmunString + "--" + new String(
Bytes.toString(v11, lengthCells.getValueOffset(), lengthCells.getValueLength())));
break;
case "eIntType":
case "eUIntType":
byte[] v111 = lengthCells.getValueArray();
if (print)
System.out.println(colmunString + "--" + (Bytes.toInt(v111, lengthCells.getValueOffset())));
break;
case "eFloatType":
byte[] v1111 = lengthCells.getValueArray();
if (print)
System.out.println(
colmunString + "--" + (Bytes.toFloat(v1111, lengthCells.getValueOffset())));
break;
default:
break;
}
}
}
}
}
}
|