📄 importsvg.java.svn-base
字号:
package test;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection;
import com.esri.arcgis.carto.AnnotationFeatureClassDescription;
import com.esri.arcgis.carto.AnnotationVBScriptEngine;
import com.esri.arcgis.carto.ElementCollection;
import com.esri.arcgis.carto.FDOGraphicsLayerFactory;
import com.esri.arcgis.carto.FillSymbolStyleGalleryClass;
import com.esri.arcgis.carto.GraphicsLayerScale;
import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.IFDOGraphicsLayer;
import com.esri.arcgis.carto.IFDOGraphicsLayerFactory;
import com.esri.arcgis.carto.ITextElement;
import com.esri.arcgis.carto.LabelEngineLayerProperties;
import com.esri.arcgis.carto.TextElement;
import com.esri.arcgis.datasourcesGDB.SdeWorkspaceFactory;
import com.esri.arcgis.display.CartographicLineSymbol;
import com.esri.arcgis.display.IFillSymbol;
import com.esri.arcgis.display.IFillSymbolProxy;
import com.esri.arcgis.display.ILineSymbol;
import com.esri.arcgis.display.ILineSymbolProxy;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SymbolCollection;
import com.esri.arcgis.display.TextSymbol;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.FeatureCursor;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.GeometryDef;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureBuffer;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureDataset;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFeatureWorkspaceAnno;
import com.esri.arcgis.geodatabase.IFeatureWorkspaceAnnoProxy;
import com.esri.arcgis.geodatabase.IFieldEdit;
import com.esri.arcgis.geodatabase.IFieldsEdit;
import com.esri.arcgis.geodatabase.IGeometryDefEdit;
import com.esri.arcgis.geodatabase.IQueryFilter;
import com.esri.arcgis.geodatabase.ITransactions;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPointCollection;
import com.esri.arcgis.geometry.IPolyline;
import com.esri.arcgis.geometry.ISpatialReference;
import com.esri.arcgis.geometry.ISpatialReference3;
import com.esri.arcgis.geometry.ISpatialReferenceFactory3;
import com.esri.arcgis.geometry.ISpatialReferenceResolution;
import com.esri.arcgis.geometry.ISpatialReferenceTolerance;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.support.ms.stdole.StdFont;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.PropertySet;
import com.esri.arcgis.system.UID;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriUnits;
public class ImportSVG {
private final Log log=LogFactory.getLog(this.getClass());
private final String filename="src/test/ZJ_温州_瓯海变.fac.svg";
//private final String filename="src/test/ZJ_杭州_瓶窑变.fac.svg";
//private final String filename="src/test/ZJ_金华_双龙变.fac.svg";
private final String path_id="*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and @id=':id' and namespace-uri()='http://www.w3.org/2000/svg']";
private Document doc=null;
private SAXBuilder builder;
private Element root=null;
private XPath path;
private IFeatureWorkspace ifw;
private Integer height;
private String dsname;
private ArrayList<Double> point=new ArrayList<Double>();
private ArrayList<String> attr=new ArrayList<String>();
private ArrayList<IPoint> ipoint=new ArrayList<IPoint>();
private IFeatureDataset ifd=null;
//标签类型
public final String Bus_Layer="Bus_Layer1011";
public final String ACLineSegMent_Layer="ACLineSegMent_Layer1011";
public final String Breaker_Layer="Breaker_Layer1011";
public final String Disconnector_Layer="Disconnector_Layer1011";
public final String Transformer3_Layer="Transformer3_Layer1011";
public final String Transformer2_Layer="Transformer2_Layer1011";
public final String Compensator_Layer="Compensator_Layer1011";
public final String PT_Layer="PT_Layer1011";
public final String Link_Layer="Link_Layer1011";
public final String Text_Layer="Text_Layer1011";
private HashMap<String, HashMap<Double,IPoint>> BusLayer=new HashMap<String, HashMap<Double,IPoint>>();
//private HashMap<Double,IPoint> coordinate;
private SpatialReferenceEnvironment sRefEnv = new SpatialReferenceEnvironment();
/*private ISpatialReference isr=sRefEnv.createProjectedCoordinateSystem(
com
.esri
.arcgis
.geometry
.esriSRProjCSType
.esriSRProjCS_WGS1984UTM_10N
);*/
private ISpatialReference isr=sRefEnv.createGeographicCoordinateSystem(
com.esri.arcgis.geometry.esriSRGeoCSType.esriSRGeoCS_Beijing1954
);
private int t=10;
//线层标签名称
private ArrayList<String> LineLayer=new ArrayList<String>();
private final Namespace ns_cge=Namespace.getNamespace("cge", "http://www.cim.com");
private final Namespace ns_svg=Namespace.getNamespace("", "http://www.w3.org/2000/svg");
XMLOutputter out;
Format format = Format.getCompactFormat();
public ImportSVG() throws Exception{
builder =new SAXBuilder();
PropertySet propSet = new PropertySet();
//连接参数
propSet.setProperty("SERVER", "192.168.1.220");
propSet.setProperty("INSTANCE", "5151");
//propSet.setProperty("DATABASE", "sde");
propSet.setProperty("USER", "sde");
propSet.setProperty("PASSWORD", "sde");
propSet.setProperty("VERSION", "SDE.DEFAULT");
SdeWorkspaceFactory swf=new SdeWorkspaceFactory();
ifw=(IFeatureWorkspace)swf.open(propSet, 0);
this.dsname="ZJ_WZ_OHB1011";
LineLayer.add("Bus_Layer");
LineLayer.add("ACLineSegment_Layer");
LineLayer.add("Link_Layer");
format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
format.setIndent(" "); //缩进
out=new XMLOutputter(format);
ISpatialReferenceTolerance isrt = (ISpatialReferenceTolerance)isr;
isrt.setXYTolerance(10);
ISpatialReferenceResolution isrrl = (ISpatialReferenceResolution) isr;
isrrl.setXYResolution(true, 1);
}
public void pase() throws Exception{
ITransactions tx=(ITransactions)ifw;
tx.startTransaction();
log.info("导入开始");
doc=builder.build(new File(filename));
root=doc.getRootElement();//取得根元素
setHeight();
log.info("取得纵坐标范围:"+height);
createDataset();
log.info("创建Dataset:"+dsname);
//获得母线
log.info("开始处理母线");
path=XPath.newInstance("*[local-name()='g' and @id='Bus_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='path' and namespace-uri()='http://www.w3.org/2000/svg']");
paseLine(path,this.Bus_Layer);
log.info("母线完成");
//获得线路
log.info("开始处理线路");
path=XPath.newInstance("*[local-name()='g' and @id='ACLineSegment_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='path' and namespace-uri()='http://www.w3.org/2000/svg']");
paseLine(path,this.ACLineSegMent_Layer);
log.info("线路完成");
//获得开关
log.info("开始处理开关");
path=XPath.newInstance("*[local-name()='g' and @id='Breaker_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='use' and namespace-uri()='http://www.w3.org/2000/svg']");
paseBDCP(path,this.Breaker_Layer);
log.info("开关完成");
//刀闸
log.info("开始处理刀闸");
path=XPath.newInstance("*[local-name()='g' and @id='Disconnector_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='use' and namespace-uri()='http://www.w3.org/2000/svg']");
paseBDCP(path,this.Disconnector_Layer);
log.info("刀闸完成");
//电容
log.info("开始处理电容");
path=XPath.newInstance("*[local-name()='g' and @id='Compensator_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='use' and namespace-uri()='http://www.w3.org/2000/svg']");
paseBDCP(path,this.Compensator_Layer);
log.info("电容完成");
//PT
log.info("开始处理PT");
path=XPath.newInstance("*[local-name()='g' and @id='PT_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='use' and namespace-uri()='http://www.w3.org/2000/svg']");
paseBDCP(path,this.PT_Layer);
log.info("PT完成");
//三卷变
log.info("开始处理三卷变");
path=XPath.newInstance("*[local-name()='g' and @id='Transformer3_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/descendant::*[local-name()='use' and namespace-uri()='http://www.w3.org/2000/svg']");
paseTransformer(path,this.Transformer3_Layer);
log.info("三卷变完成");
//连接线
log.info("开始处理连接线");
path=XPath.newInstance("*[local-name()='g' and @id='Link_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']");
paseLinkLayer(path);
log.info("连接线完成");
//画母线
log.info("画母线");
this.constructBus_Layer();
log.info("母线绘制完成");
//注记
log.info("注记");
path=XPath.newInstance("*[local-name()='g' and @id='Text_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='text' and namespace-uri()='http://www.w3.org/2000/svg']");
paseTextLayer(path);
tx.commitTransaction();
log.info("完成");
}
/**
* 设置图形纵坐标
* */
private void setHeight() throws Exception{
path=XPath.newInstance("*[local-name()='g' and @id='Head_Layer' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='rect' and namespace-uri()='http://www.w3.org/2000/svg']");
Element headLayer=(Element)path.selectSingleNode(root);
height=new Integer(headLayer.getAttributeValue("height"))*t;
}
/**
* 处理母线和线路
* @param path 元素的XPath
* @param tagType 标签类型:0,母线.1,线路
* */
private void paseLine(XPath path,String tagType) throws Exception{
IGeometryDefEdit igde=new GeometryDef();
igde.setGeometryType(com.esri.arcgis.geometry.esriGeometryType.esriGeometryPolyline);
igde.setSpatialReferenceByRef(isr);
//设置列集合
IFieldsEdit pFieldsEdit = new Fields();
//设置列
IFieldEdit pFieldEdit = new Field();
IFieldEdit shp = new Field();
IFieldEdit svgid=new Field();
IFeatureClass ifc=null;
//加入基本字段
pFieldEdit.setName("ObjectID");
pFieldEdit.setType(com.esri.arcgis.geodatabase.esriFieldType.esriFieldTypeOID);
shp.setName("SHP");
shp.setType(com.esri.arcgis.geodatabase.esriFieldType.esriFieldTypeGeometry);
shp.setGeometryDefByRef(igde);
svgid.setName("SVGID");
svgid.setType(com.esri.arcgis.geodatabase.esriFieldType.esriFieldTypeInteger);
pFieldsEdit.addField(pFieldEdit);
pFieldsEdit.addField(shp);
pFieldsEdit.addField(svgid);
Iterator<Element> it=path.selectNodes(root).iterator();
Element e=null;
if(tagType==this.Bus_Layer){
// 加入母线特有属性
log.info("创建母线层:"+Bus_Layer);
ifc=createFeatureClass(this.Bus_Layer,pFieldsEdit);
log.info("记录母线端点坐标:"+Bus_Layer);
while(it.hasNext()){
e=it.next();
//加入母线HashMap并放入两个端点的坐标
BusLayer.put(e.getParentElement().getAttributeValue("id"), new HashMap<Double,IPoint>());
BusLayer.get(e.getParentElement().getAttributeValue("id")).put(pasePath(e.getAttributeValue("d")).getFromPoint().getX(), pasePath(e.getAttributeValue("d")).getFromPoint());
BusLayer.get(e.getParentElement().getAttributeValue("id")).put(pasePath(e.getAttributeValue("d")).getToPoint().getX(), pasePath(e.getAttributeValue("d")).getToPoint());
}
}else if(tagType==this.ACLineSegMent_Layer){
//加入线路特有属性
ifc=createFeatureClass(this.ACLineSegMent_Layer,pFieldsEdit);
log.info("创建线路层:"+this.ACLineSegMent_Layer);
FeatureClass line=new FeatureClass(ifc);
FeatureCursor fc=new FeatureCursor(line.insert(true));
IFeatureBuffer fb=line.createFeatureBuffer();
//SVGID字段序列
int index_svgid=fb.getFields().findField("SVGID");
while(it.hasNext()){
e=it.next();
fb.setShapeByRef(pasePath(e.getAttributeValue("d")));
fb.setValue(index_svgid, e.getParentElement().getAttributeValue("id"));
//log.info("导入SVG图中母线,ID:"+e.getParentElement().getAttributeValue("id"));
fc.insertFeature(fb);
}
fc.flush();
}
}
/**
* 构建母线
* */
private void constructBus_Layer() throws Exception{
FeatureClass line=new FeatureClass(ifw.openFeatureClass(this.Bus_Layer));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -