query-sql
本文回答:
svc.conditionQueryFeature/pointQueryFeature/rectQueryFeature的用法、字段表、常见 SQL(速查版)。完整文档 ⇒ query-features(包含全部 36 个
name='xx'类型、s3/s4/s5/n5~n10 在每种实体下的字段含义、详细 SQL 示例、绘制姿势)。本文只是 API 速查。
一、前置条件
几何渲染方式打开的图才有空间数据库可查:
await svc.openMap({ mapid, mapopenway: vjmap.MapOpenWay.GeomRender });
内存模式无法用 SQL;改用 query-expression。
二、表结构(只写 where 后面部分)
| 字段 | 类型 | 说明 |
|---|---|---|
id |
int | 自增 ID |
objectid |
text | 实体 ID(块/组/表格有特殊命名规则,详见 query-features 第二节) |
layerindex |
int | 图层索引 |
name |
text | 实体类型值(字符串),如 '1' = AcDbLine。完整 36 类见 query-features 第三节 |
color |
int | 颜色(RGB + Alpha,多为负数) |
s1..s5 |
text | 字符型属性。s1=线型;s2=扩展字典;s3..s5 含义随类型变化 |
n1..n10 |
float | 数值型属性。n1=颜色索引;n2=线型比例;n3=线宽;n4=透明度;n5..n10 含义随类型变化 |
geom |
GEOMETRY | 实体外包矩形(用于空间查询) |
⚠️
s3..s5/n5..n10的具体含义因实体类型而异(圆的n7=radius,直线的n6=length...)。完整对照见 query-features 第四节。
三、条件查询 conditionQueryFeature
const res = await svc.conditionQueryFeature({
condition: `name='13' or name='12'`, // SQL where 部分
fields: "objectid,text,envelop", // 要返回的字段
geom: true, // 走几何渲染空间库(不是"返回几何";缺省取当前图打开方式)
limit: 100000, // 默认 100,需显式放大
bounds: [x1,y1,x2,y2], // 可选:矩形或点数组(多边形)
isContains: false, // 范围查询时:false=相交/true=包含
includegeom: true, realgeom: true // 要真实 geojson:两者同传(只传 realgeom 无效),且 fields 须含 geojson;数量少时用
});
// res.recordCount, res.result[]
分页
let begin = 0, all = [];
while (true) {
const q = await svc.conditionQueryFeature({ condition, fields, limit: 10000, beginpos: begin });
all.push(...(q.result || []));
begin += q.result.length;
if (all.length >= q.recordCount) break;
}
常用 SQL 示例(更多见 query-features 第五节)
name='1' // 所有直线
name='4' and layerindex=0 // 0 图层的三维多段线
name='13' and color=-16776961 // 红色单行文字(0xFF0000FF)
objectid='9BE' // 指定 objectid
name='13' or name='12' or name='26' or name='27'
// 所有文字(单行/多行/属性注记/块属性)
objectid like 'EA_%' // 指定块下的子实体
name='1' and n6 = 10 // 长度 10 的直线(n6=length)
name='7' and n7 = 5 // 半径 5 的圆(n7=radius)
name='10' and s5='门' // 块名为"门"的所有块参照(s5=blockname)
name='13' and s4 like 'DZ%' // 文字内容以 DZ 开头(s4=text)
name='2' and n6=1 // 闭合的多段线(n6=isclosed)
四、点查询 pointQueryFeature
const q = await svc.pointQueryFeature({
x: 3200, y: 1800,
pixelsize: 5,
condition: `name='7'`, // 可选进一步过滤
zoom: map.getZoom(), // 用于 pixel→几何长度
limit: 20,
geom: true,
toMapCoordinate: true // 直接返回 CAD 坐标
});
典型场景:鼠标点击选实体。
五、矩形 / 多边形查询 rectQueryFeature
// 矩形
await svc.rectQueryFeature({
x1, y1, x2, y2,
condition: `name='7'`,
limit: 100000,
geom: true
});
// 多边形:用 conditionQueryFeature + bounds 数组
await svc.conditionQueryFeature({
condition: "name='7'",
bounds: [[p1x,p1y],[p2x,p2y],[p3x,p3y],[p4x,p4y]],
isContains: true
});
六、把查询结果绘制到地图
const polygons = q.result.map(r => {
const bounds = map.getEnvelopBounds(r.envelop);
return {
points: map.toLngLat(bounds.toPointArray()),
properties: {
name: r.objectid,
color: vjmap.entColorToHtmlColor(r.color),
text: r.text
}
};
});
const layer = new vjmap.Polygon({
data: polygons,
fillColor: ["get", "color"],
fillOpacity: 0.4,
fillOutlineColor: "#fff"
});
layer.addTo(map);
七、查询返回字段速查
常用:objectid / bounds / envelop / points / center / radius / text / contents / blockname / area / length。
完整字段表与各类型字段对照见 query-features 第八节。
八、常见坑
name=1写成不带引号 →name是字符串字段,必须name='1'。- 没传
limit→ 默认只给 100 条(点查询只给 1 条),业务上误判为"查不全"。 realgeom: true但数量巨大 → 流量爆炸。建议仅在单条详情时使用。isContains: true遗漏实体:矩形包含很严格,轻微溢出就不算包含。通常false(相交)更符合直觉。- 拼 SQL 时用户输入没转义 → 注入风险。SDK 没内置防护,要么自己转义,要么只允许白名单字段。
更多坑见 query-features 第十二节。
九、相关参考
- 完整查询文档 ⇒ query-features(实体类型表、字段含义、详细 SQL)
- 表达式查询(内存模式 / 复杂逻辑)⇒ query-expression
- 综合用例(自动分图 / 提表 / 中心线)⇒ query-cases
- 全文搜索 ⇒ query-fulltext
- 服务端样式过滤 ⇒ svr-style-var