叠加Shp数据。
注意事项
Shp数据在Skyline Web中显示的时候有部分属性是缺失的,需要编码调整后才能正常显示,且需要注意是叠加在底图上,需要把三维模型取消掉才能看得到,否则会被压盖住。
因为需要在Shp数据的面上做点击弹出事件,如果一个Shp数据含多个点或面等,需要使用到GeoTools开源工具包在内部解析多个中心坐标点位,因为Skyline中自带只能解析一个中心点位,被打脸...,在Skyline中也带有可以获取多个点或面的方法,详见API。
这个案例中的Shp数据从本地加载,可以改成网络发布。
需求分解
- 构建特征数据
- 构建信息显示窗口
方法说明
- SGWorld66.Creator.CreateFeatureLayer(layerName,sConnectionString,GroupID);
- 创建特征层,layerName为名称,sConnectionString为数据源,GroupID为目录树组。sConnectionString为数据源的组织方式,可以加载多种数据,详见API。
- 需要注意的是:创建完该对象后,需要配置IgnoreZ=false;属性和Refresh();方法,才能正常显示。
- SGWorld66.Creator.CreateMessage(TargetPosition,msgData,Type,BringToFront);
- 创建信息对象,后通过属性关联。我们这里采用了信息的第五个类型,简单的POPUP对象,如需要完整的POPUP对象(可设置Caption),需要创建CreatePopupMessage对象来完成。
Jsp源码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="com.thinkgem.jeesite.common.utils.SpringContextHolder"%>
<%@ page import="com.thinkgem.jeesite.common.test.ShpTest"%>
<%@ page import="com.vividsolutions.jts.geom.Point"%>
<%
ShpTest shpTest = SpringContextHolder.getBean(ShpTest.class);
List<Point> pointList = shpTest.readSHP("F:\\ShpTest3\\wj.shp");
%>
<html>
<script type="text/javascript" >
/**
* 初始化
*/
function Init() {
try {
//文件地址
var flyPath = "F:\\YFSY0519\\YFSY0519.fly";
//打开工程
SGWorld66.Project.Open(flyPath);
var shpLayer = SGWorld66.Creator.CreateFeatureLayer("基底图地形","FileName=F:\\ShpTest2\\基底图地形.shp;TEPlugName=OGR;");
shpLayer.IgnoreZ = false;
shpLayer.Refresh();
var labelStyle = createLabelStyle(0xFF0000,0,"微软雅黑",20,200,true);
var shpLayer1 = SGWorld66.Creator.CreateFeatureLayer("违章建筑1-SHP","FileName=F:\\ShpTest3\\wj.shp;TEPlugName=OGR;");
shpLayer1.IgnoreZ = false;
shpLayer1.Refresh();
shpLayer1.FeatureGroups.SetProperty("Fill Opacity","100%");
shpLayer1.FeatureGroups.SetProperty("Fill Color","#00ff00");
<% for(int i=0;i<pointList.size();i++){
Point p = pointList.get(i);
%>
var p<%=i%> = SGWorld66.Creator.CreatePosition(<%=p.getX()%>,<%=p.getY()%>,0,0,0,0);
var ctl<%=i%> = SGWorld66.Creator.CreateTextLabel(p<%=i%>, "违章建筑" + <%=i%>, labelStyle, "", "违章建筑" + <%=i%>);
ctl<%=i%>.Message.MessageID = SGWorld66.Creator.CreateMessage(5,"建筑时间:20170666",0).ID;
<% } %>
var shpLayer2 = SGWorld66.Creator.CreateFeatureLayer("违章建筑-SHP","FileName=F:\\ShpTest3\\wj2.shp;TEPlugName=OGR;");
shpLayer2.IgnoreZ = false;
shpLayer2.Refresh();
shpLayer2.FeatureGroups.SetProperty("Fill Opacity","100%");
shpLayer2.FeatureGroups.SetProperty("Fill Color","#6f11ee");
var ctl2 = SGWorld66.Creator.CreateTextLabel(shpLayer2.Position, "违章建筑", labelStyle, "", "违章建筑");
ctl2.Message.MessageID = SGWorld66.Creator.CreateMessage(5,"建筑时间:20170612",0).ID;
} catch (e) {
alert("Error: " + e.description);
}
}
/**
*设置文本标注样式
*/
function createLabelStyle(nBGRValue,dAlpha,FontName,FontSize,Scale,Bold){
// C1. Create label syle
var cLabelStyle = SGWorld66.Creator.CreateLabelStyle();
// C2. Change label style settings
var nBGRValue = nBGRValue; // Blue
var dAlpha = dAlpha; // 50% opacity
var cBackgroundColor = cLabelStyle.BackgroundColor; // Get label style background color
cBackgroundColor.FromBGRColor(nBGRValue); // Set background to blue
cBackgroundColor.SetAlpha(dAlpha); // Set transparency to 50%
cLabelStyle.BackgroundColor = cBackgroundColor; // Set label style background color
cLabelStyle.FontName = FontName; // Set font name
cLabelStyle.FontSize = FontSize; // Set font size
cLabelStyle.Italic = false; // Set label style font to italic
cLabelStyle.Scale = Scale; // Set label style scale
cLabelStyle.Bold = Bold;
//cLabelStyle.MaxViewingHeight = MaxViewingHeight;
cLabelStyle.LimitScreenSize = true;
cLabelStyle.LineToGround = true;
//cLabelStyle.MinViewingHeight = MinViewingHeight;
return cLabelStyle;
}
function Even(){
}
</script>
<style>
.btn{
border: none;
display: inline-block;
outline: 0;
padding: 6px 16px;
margin-bottom: 10px;
vertical-align: middle;
overflow: hidden;
text-decoration: none;
color: #fff;
background-color: #3385ff;
text-align: center;
transition: .2s ease-out;
cursor: pointer;
white-space: nowrap;
width: 100%;
margin-top: 5px;
}
</style>
<body onload = "Init();" style="background-color: black;padding:0; margin:0;overflow: hidden;">
<div style="position: absolute;width: 80%;height: 100%;left: 0%;">
<!--定义一个TerraExplorer 3D窗口对象-->
<object id="TE" classid="clsid:3a4f9192-65a8-11d5-85c1-0001023952c1" style="width: 100%;height: 100%;padding:0; margin:0;"></object>
</div>
<div style=" position: absolute;width: 18%;height: 99%;left: 81%;">
<!--定义一个TerraExplorer 信息窗口对象-->
<object id="TEInformationWindow" classid="CLSID:3a4f9193-65a8-11d5-85c1-0001023952c1" style="width: 100%;height: 50%;"></object>
<!--定义TerraExplorer对象SGWorld66-->
<object id="SGWorld66" classid="CLSID:3a4f9199-65a8-11d5-85c1-0001023952c1" style="visibility:hidden;height:0;"></object>
<div>
<input id="Button1" value="示例按钮-编辑事件" onclick="Even()" type="button" class="btn"/>
</div>
</div>
</body>
</html>
Java源码
package com.thinkgem.jeesite.common.test;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureSource;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
@Service
@Transactional(readOnly = true)
public class ShpTest {
public static List<Point> readSHP(String path) {
ShapefileDataStore shpDataStore = null;
List<Point> pointList = new ArrayList<Point>();
try{
shpDataStore = new ShapefileDataStore(new File(path).toURI().toURL());
shpDataStore.setStringCharset(Charset.forName("GBK"));
String typeName = shpDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
System.out.println(result.size());
FeatureIterator<SimpleFeature> itertor = result.features();
while(itertor.hasNext()){
SimpleFeature feature = itertor.next();
Collection<Property> p = feature.getProperties();
Iterator<Property> it = p.iterator();
while(it.hasNext()) {
Property pro = it.next();
if (pro.getValue() instanceof Point) {
System.out.println("PointX = " + ((Point)(pro.getValue())).getX());
System.out.println("PointY = " + ((Point)(pro.getValue())).getY());
} if(pro.getValue() instanceof MultiPolygon){
MultiPolygon mp = (MultiPolygon)(pro.getValue());
System.out.println(mp.getCentroid());
pointList.add(mp.getCentroid());
} else {
System.out.println(pro.getName() + " = " + pro.getValue());
}
}
}
itertor.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) { e.printStackTrace(); }
return pointList;
}
}
Maven
GeoTools采用的2.7.2版本。
<repository>
<id>geotools-repos</id>
<name>GeoTools Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<id>geotools2-repos</id>
<name>GeoTools2 Repository</name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
<!-- GEOTOOLS begin -->
<!-- https://mvnrepository.com/artifact/org.geotools/gt-api -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>2.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-data -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-metadata</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>2.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.measure/jsr-275 -->
<dependency>
<groupId>javax.measure</groupId>
<artifactId>jsr-275</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.vividsolutions/jts -->
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>2.7.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/gt-referencing-2.7.2.jar</systemPath>
</dependency>
<!-- GEOTOOLS end -->
效果图