⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addpointlabel.java

📁 CreateMyFirstMap
💻 JAVA
字号:
package myfirstmap;
import com.mapinfo.mapxtreme.client.MapXtremeDataProviderRef;
import com.mapinfo.xmlprot.mxtj.ImageRequestComposer;
import java.awt.Color;
import com.mapinfo.mapj.MapJ;
import com.mapinfo.unit.LinearUnit;
import com.mapinfo.util.DoublePoint;
import com.mapinfo.util.DoubleRect;
import com.mapinfo.graphics.Rendition;
import com.mapinfo.graphics.RenditionImpl;
import com.mapinfo.dp.Attribute;
import com.mapinfo.dp.PrimaryKey;
import com.mapinfo.mapj.FeatureFactory;
import com.mapinfo.dp.Feature;
import com.mapinfo.mapj.Layer;
import com.mapinfo.mapj.Layers;
import com.mapinfo.mapj.AbstractLayer;
import com.mapinfo.mapj.FeatureLayer;
import com.mapinfo.mapj.DataLayer;
import com.mapinfo.dp.TableInfo;
import com.mapinfo.dp.util.TableInfoImpl;
import com.mapinfo.coordsys.CoordSys;
import com.mapinfo.dp.annotation.AnnotationDataProviderHelper;
import com.mapinfo.dp.annotation.AnnotationTableDescHelper;
import com.mapinfo.mapj.BaseLabelProperties;
import com.mapinfo.dp.util.LocalDataProviderRef;
import java.util.Vector;
import com.mapinfo.dp.util.RewindableFeatureSet;
import com.mapinfo.mapxtreme.client.MapXtremeImageRenderer;
import com.mapinfo.mapj.LabelProperties;
import com.mapinfo.unit.Distance;
public class AddPointLabel
{
 private String mdfFilePath="e:\\MapInfo\\study\\mapmdf";
 private  String mdfFileName="states.mdf";
 private String mapxtremeURL="http://901-32:8083/mapxtreme47/mapxtreme";
 private int NUM_OF_COLORS = ImageRequestComposer.MAX_COLORS_TRUECOLOR;
 private Color BACKGROUND_COLOR = Color.white;
  private String layerName;
  public AddPointLabel()
  {
  }
public void addLabelToPoint()
  {
    MapJ mapj=new MapJ();
    Layers layers;
    DataLayer dataLayer;
    DoublePoint centerPoint=new DoublePoint(-108.8,42.5);
    LinearUnit unit=LinearUnit.kilometer ;
    mapj.setDistanceUnits(unit) ;
    mapj.setDeviceBounds(new DoubleRect(0,0,800,600)) ;
    Rendition rend = RenditionImpl.getDefaultRendition();
    rend.setValue(Rendition.SYMBOL_MODE, Rendition.SymbolMode.FONT);
    rend.setValue(Rendition.FONT_FAMILY, "MapInfo Cartographic");
    rend.setValue(Rendition.FONT_SIZE, 6);
    rend.setValue(Rendition.SYMBOL_STRING, String.valueOf((char)33));
    //  Rendition labelR.end = RenditionImpl.getDefaultRendition();        //required by feaFac constructor
    Attribute fAtt[] = new Attribute[3];	 //required by feaFac constructor
    fAtt[0] = new Attribute("colvalue1");
    fAtt[1]=new Attribute("colvalue2");
    fAtt[2]=new Attribute("colvalue3");
    //fAtt[1]=new Attribute("pkeyname");
    PrimaryKey pkey = new PrimaryKey(fAtt); //required by feaFac constructor(below)
    FeatureFactory feaFac = mapj.getFeatureFactory();
    try
    {  mapj.loadMapDefinition(mdfFilePath+"\\"+mdfFileName);
      layers=mapj.getLayers() ;
      dataLayer=(DataLayer)layers.get(0) ;
      DoubleRect    rect=dataLayer.getBounds() ;
      double width=(rect.xmax -rect.xmin )/4;
      double height=(rect.ymax -rect.ymin )/4;
      mapj.setBounds(new DoubleRect(centerPoint,width,height)) ;//set the map to the size of 16*orginal size
      /*创建一个点几何体,它有三个属性,到底有多少个属性由自己设定但一定要和后面的表结构一致,一个几何体在一个表中就相当
         于一行;这儿用的pkey即主关键字,实际上它用到了三列,我们可以用一列,二列,三列都可以*/
      Feature feature = feaFac.createPoint(centerPoint, null, null, fAtt,pkey);
      //
      //下面这个是练习,练习如何得到feature对应的列值,因为一个层实际上对应的就是一个表,所以一个feature
      //对应的应该就是一列
      for (int i = 0; i < feature.getAttributeCount(); i++)
      {
        Attribute att = feature.getAttribute(i);
        String str = att.getString();
        str = "wait";
      }

      Layer layer = null;
      String[] ticolumns = new String[3];
      ticolumns[0] = "colname1";
      ticolumns[1]="colname2";
      ticolumns[2] = "colname3";
      //to be the primary key
      int[] ctypes = new int[3];
      ctypes[0] = TableInfo.COLUMN_TYPE_STRING;		//specifies data-type in zeroth column
      ctypes[1] = TableInfo.COLUMN_TYPE_STRING;         //specifies data-type in first column
      ctypes[2]=TableInfo.COLUMN_TYPE_STRING ;
      int[] primarykey = new int[3];
      primarykey[0]=0;
      primarykey[1]=1;
      primarykey[2]=2;
     /*下面这条语句实际上就是定义一个表结构,从ticolumns可知它的三个列名,从ctypes可以知道每列的类型,
      从3可以知道这个表有三列,记住ticolumns的大小必须和第六个参数一致,这儿为3,最难理解的是primarykey,经过我的
      试验我发现它是指用哪几列作为主关键字列,这儿用了这三列*/
     TableInfoImpl tii =
         new TableInfoImpl("tablename", CoordSys.longLatNAD83, ticolumns,
                           ctypes, 3,primarykey, false);
     AnnotationTableDescHelper annTDHelper = new AnnotationTableDescHelper("tablename");
     AnnotationDataProviderHelper annDPHelper = new AnnotationDataProviderHelper(tii);
     LocalDataProviderRef localDPRef = new LocalDataProviderRef(annDPHelper);
     layer = mapj.getLayers().insert(localDPRef, annTDHelper, 0, "layernameforlabel");//add this
     //annotation layer to mapj instance

     //得到层名,没有其它的意义,只是为了学习
//     Layers layers=mapj.getLayers() ;

     for(int layerindex=0;layerindex<layers.size() ;layerindex++)
     {
       AbstractLayer alayer=layers.get(layerindex) ;
       FeatureLayer featureLayer=(FeatureLayer)layers.get(layerindex) ;
       featureLayer.setAutoLabel(true) ;
       layerName= alayer.getName() ;
       //    labelProperties.setPriority(10) ;
//    int priority= labelProperties.getPriority() ;
     }
     //layer.setVisible(false);
     layer.addFeature(feature);//把这点增加到该层中去
     //create another feature again,if we use the previous fatt value,an error will occur,becuase these
     //value will be used as the primary key at the same time
     fAtt[0]=new Attribute("colvalue11");
     fAtt[1]=new Attribute("colvalue12");
     fAtt[2]=new Attribute("colvalue13");
     centerPoint=new DoublePoint(-109.8,43.5);
     //I don't know why it can be passed with the pkey unchanged
     feature = feaFac.createPoint(centerPoint, null, null, fAtt,pkey);
     //下面这个是练习,练习如何得到feature对应的列值,因为一个层实际上对应的就是一个表,所以一个feature
     //对应的应该就是一列
     for(int i=0;i<feature.getAttributeCount() ;i++)
     {
       Attribute att=feature.getAttribute(i) ;
       String str=att.getString() ;
       str="wait";
     }

     layer.addFeature(feature) ;//add this feature to annotation layer
    //set label style
    LabelProperties labelProperties=layer.getLabelProperties() ;
    Rendition labelRend=labelProperties.getRendition() ;
    labelRend.setValue(Rendition.FONT_WEIGHT,2f);
    labelRend.setValue(Rendition.SYMBOL_FOREGROUND ,Color.blue  );
    labelRend.setValue(Rendition.FILTER_EFFECTS ,Rendition.FilterEffects .HALO );
    labelRend.setValue(Rendition.FONT_STYLE ,Rendition.FontStyle .ITALIC );
    labelProperties.setRendition(labelRend) ;
    //create a scalable label
    Distance distance=new Distance(28,LinearUnit.kilometer );
    labelRend.setValue(labelRend.FONT_SIZE ,distance);
    labelProperties.setMultiLineTextMode(LabelProperties.MULTILINE_TEXT_ON ) ;
    labelProperties.setGeometryCalculationMode(LabelProperties.GEOMETRY_COMPUTED ) ;
  //  labelProperties.setZoomMax(10,LinearUnit.kilometer ) ;
    //labelProperties.setZoomMin(5,LinearUnit.kilometer ) ;
     //blp.setLabelExpression("\"colname:\""+ticolumns[0]);
     //tell the lable to how to display information for us
     System.out.println("labelProperties.DEFAULT_OFFSET .x ="+labelProperties.DEFAULT_OFFSET .x);
    System.out.println("labelProperties.DEFAULT_OFFSET .y ="+labelProperties.DEFAULT_OFFSET .y);
     BaseLabelProperties blp = new BaseLabelProperties(labelProperties);
//     blp= layer.getLabelProperties();
     blp.setLabelExpression("\"colname1:[\"+colname1+\"]\\ncolname2:[\"+colname2+\"]colname3:[\"+colname3+\"]\"");
     layer.setLabelProperties(blp);
    // layer.setLabelProperties(labelProperties) ;
     mapj.setZoom(800000) ;
     ImageRequestComposer imageRC =
         ImageRequestComposer.create(mapj, 256, Color.white , "image/png");
     MapXtremeImageRenderer render = new MapXtremeImageRenderer(mapxtremeURL);
     render.render(imageRC);
     render.toFile("E:\\MapInfo\\study\\png\\labelProperty3.png") ;
     render.dispose() ;
   }catch(Exception e)
   {
     System.out.println("get a "+e.getClass() +"\nwith message "+e.getMessage() ) ;
   }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -