📄 featuretest.java
字号:
package barchant;import java.util.*;import com.mapinfo.dp.*;import com.mapinfo.util.*;import com.mapinfo.beans.vmapj.VisualMapJ;import com.mapinfo.mapj.FeatureLayer;import com.mapinfo.dp.util.RewindableFeatureSet;/*该示例演示了有关图元的一些方法,成功*/public class FeatureTest { public FeatureLayer world;//Feature对象用来访问地图对象 public VisualMapJ myMap;//可管理地图集的各种状态,并向服务器转发客户请求 public FeatureTest() { List columns = new ArrayList(); Feature ftr; Geometry geom; DoubleRect rect; DoublePoint dblPnt; PointGeometry pntGeometry; VectorGeometry vectorGeometry; PointList pntList; Attribute attrib; int attribCount; try{ //创建地图 myMap=new VisualMapJ(); myMap.getMapJ().loadMapDefinition("d:\\program files\\MapInfo\\MapXtreme-4.7.0\\examples\\server\\data\\local\\world.mdf"); //mdf是地图定义文件 world=(FeatureLayer)myMap.getMapJ().getLayers().get("World Countries");//得到World Countries图层 // Get the Table information from the FeatureLayer TableInfo tabInfo = world.getTableInfo(); // fill vector with Column names // System.out.println("tableColumnCount="+tabInfo.getColumnCount()); for (int i=0;i<tabInfo.getColumnCount();i++) { columns.add(tabInfo.getColumnName(i)); // System.out.println("columnName="+tabInfo.getColumnName(i)); } // 搜索有几条纪录,放到可回读的图元集中 RewindableFeatureSet rFtrSet = new RewindableFeatureSet(world.searchAll(columns, null)); // 得到第一个图元,图元集中的图元没有顺序 ftr=rFtrSet.getNextFeature(); // then loop through all features in the layer while (ftr!=null) {/* get the first attribute (columnData) from the feature Note: Ifyou want to re-use the Attribute object later on (after thegetNextFeature loop), you would need to make a copy of theAttribute object, using the copy constructor.*/ //得到一个图元的第一个属性 attrib = ftr.getAttribute(0); System.out.println("attribute="+attrib.toString()); //得到一个图元的属性数目 attribCount = ftr.getAttributeCount(); System.out.println("attributeCount="+attribCount); //得到几何信息 geom = ftr.getGeometry(); //如果该元素是点 if (geom.getType()==Geometry.TYPE_POINT) { System.out.println("aaaaaaaaaaaa"); //把该元素转换成点元素 pntGeometry = (PointGeometry)geom; //得到该点元素的边界 rect = pntGeometry.getBounds(); System.out.println(rect.toString()); //得到该点元素的标注位置 dblPnt = pntGeometry.getLabelPoint(null); } else //如果不是点元素,就可用VectorGeometry来存储 { //将元素转换成 Vector geometry vectorGeometry = (VectorGeometry)geom; // get the minimum bounding rectangle for the feature rect = vectorGeometry.getBounds(); // get the x,y location where the feature’s label will be anchored dblPnt = vectorGeometry.getLabelPoint(null); System.out.println("dblPnt:"+dblPnt.toString()); double[] pnts; int offset=0; int numPts;// Loop through all the point groups and then put the points into an array //无论是线元素还是区域都是由点构成的 for (int i=0;i<vectorGeometry.getPointListCount();++i) { // Get the next Point List pntList = vectorGeometry.getNextPointList(); // determine the number of Points in the point group numPts = pntList.getPointCount(); // Create the point array large enough to hold all the points pnts = new double[numPts]; // Call getNextPoints which will put the points into the array pntList.getNextPoints(pnts, offset, numPts/2); //System.out.println("numPts="+numPts); } } // Get the next feature ftr=rFtrSet.getNextFeature(); } // Rewind the FeatureSet to prepare for future use重新回到第一个图元 rFtrSet.rewind(); }catch(Exception e){ System.out.println("Error"); e.printStackTrace(); } } public static void main(String args[]){ FeatureTest bclr=new FeatureTest(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -